-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from MailOnline/feat/various
Feat/various
- Loading branch information
Showing
35 changed files
with
954 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
language: node_js | ||
os: | ||
- linux | ||
cache: | ||
yarn: true | ||
directories: | ||
- ~/.npm | ||
notifications: | ||
email: false | ||
node_js: | ||
- '9' | ||
- '8' | ||
script: | ||
- npm run test | ||
- npm run build | ||
matrix: | ||
allow_failures: [] | ||
fast_finish: true | ||
after_success: | ||
- npm run semantic-release | ||
branches: | ||
except: | ||
- /^v\d+\.\d+\.\d+$/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# `<Alert>` | ||
|
||
Uses [`Window.alert()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert) to display message ot a user. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {Alert} from 'libreact/lib/Alert'; | ||
|
||
<Alert show text='Hello world' /> | ||
``` | ||
|
||
## Props | ||
|
||
- `show` - boolean, optional, whether to show the alert. | ||
- `text` - string, require, string message to display to the user. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# `<OrientationSensor>` | ||
|
||
Tracks screen orientation using [`orientationchange` event](https://developer.mozilla.org/en-US/docs/Web/Events/orientationchange). | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {OrientationSensor} from 'libreact/lib/OrientationSensor'; | ||
|
||
<OrientationSensor>(state) => | ||
<pre>{JSON.stringify(state, null, 4)}</pre> | ||
</OrientationSensor> | ||
``` | ||
|
||
## Props | ||
|
||
None. | ||
|
||
## State | ||
|
||
Has signature | ||
|
||
```ts | ||
interface IOrientationSensorState { | ||
angle: number; | ||
type: string; | ||
} | ||
``` | ||
|
||
, where | ||
|
||
- `angle` - screen rotation angle in degrees. | ||
- `type` - is one of `portrait-primary`, `portrait-secondary`, `landscape-primary`, or `landscape-secondary`. | ||
|
||
|
||
## `withOrientation()` | ||
|
||
HOC that injects `orientation` object into your component. | ||
|
||
|
||
```js | ||
import {withOrientation} from 'libreact/lib/OrientationSensor'; | ||
|
||
const MyCompWithOrientation = withOrientation(MyComp); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# `<Prompt>` | ||
|
||
Uses [`Window.prompt()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) to get user input. | ||
|
||
## Usage | ||
|
||
Use it as a standalone compnent that returns result in `onResult` handler | ||
|
||
```jsx | ||
import {Prompt} from 'libreact/lib/Prompt'; | ||
|
||
<Prompt | ||
show | ||
message='Hello world' | ||
default='hello back' | ||
onResult={console.log} | ||
/> | ||
``` | ||
|
||
Or use it as a FaCC | ||
|
||
```jsx | ||
<Prompt | ||
show | ||
message='Hello world' | ||
default='hello back' | ||
onResult={console.log} | ||
>{(result) => <div>{result}</div>}</Prompt> | ||
``` | ||
|
||
## Props | ||
|
||
- `show` - boolean, optional, whether to show the prompt modal. | ||
- `message` - string, optional, string message to display to the user. | ||
- `default` - string, optional, default text to pre-fill the user's response input. | ||
- `onResult` - function, optional, function that receives prompt result string as a single argument. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# `<ViewportSensor>` | ||
|
||
Tracks if `children` are in viewport. Can be used as FaCC or a regular component. | ||
|
||
Under-the-hood it uses `<ViewportObserverSensor>`, which in turn uses [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver) API | ||
to detect if your component is in viewport. | ||
|
||
If `IntersectionObserver` API is not available it falls back and lazily downloads `<ViewportScrollSensor>`, | ||
which uses document's scroll event to track if your component is in viewport. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {ViewportSensor} from 'libreact/lib/ViewportSensor'; | ||
|
||
<ViewportSensor threshold={1} onChange={console.log}>{(state) => | ||
<pre>{JSON.stringify(state, null, 4)}</pre> | ||
}</ViewportSensor> | ||
``` | ||
|
||
## Props | ||
|
||
Sensor's props have the following signature | ||
|
||
```ts | ||
interface IViewportSensorProps { | ||
threshold?: number; | ||
onChange?: (state: IViewportObserverSensorState) => void; | ||
} | ||
``` | ||
|
||
, where | ||
|
||
- `threshold` - optional, number, percentage how much does your component have to intersect with viewport | ||
to be considered visible. Defaults to `0`. | ||
- `onChange` - optional, callback called when sensor changes its state, receives the state of the sensor as | ||
a single argument. | ||
|
||
## State | ||
|
||
Sensor's state has the following signature | ||
|
||
```ts | ||
interface IViewportSensorState { | ||
visible: boolean; | ||
} | ||
``` | ||
|
||
## `<ViewportScrollSensor>` | ||
|
||
The `<ViewportScrollSensor>` has an additional prop `throttle`, which is a number in milliseconds specifying | ||
how much to throttle document's `scroll` event. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# `getDisplayName()` | ||
|
||
Returns display name of a React component. | ||
|
||
```jsx | ||
import getDisplayName from 'libreact/lib/util/getDisplayName'; | ||
|
||
const name = getDisplayName(MyComponent); | ||
``` | ||
|
||
Accepts a single argument, which can be a stateful or stateless React component, or element. Returns a string. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {createElement as h} from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {linkTo} from '@storybook/addon-links'; | ||
import {Alert} from '..'; | ||
import ShowDocs from '../../../.storybook/ShowDocs' | ||
|
||
storiesOf('Generators/Alert', module) | ||
.add('Documentation', () => h(ShowDocs, {name: 'Alert'})) | ||
.add('Basic example', () => <Alert show text='Hello world' />) | ||
.add('Don\'t show', () => <Alert show={false} text='You shall not see this' />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {PureComponent} from 'react'; | ||
import {noop, isClient} from '../util'; | ||
|
||
export interface IAlertProps { | ||
show?: boolean; | ||
text?: string; | ||
} | ||
|
||
export class Alert extends PureComponent<IAlertProps, any> { | ||
componentDidMount () { | ||
this.alert(); | ||
} | ||
|
||
componentDidUpdate () { | ||
this.alert(); | ||
} | ||
|
||
alert () { | ||
const {show, text} = this.props; | ||
|
||
if (show) { | ||
alert(text); | ||
} | ||
} | ||
|
||
render () { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {createElement as h} from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {linkTo} from '@storybook/addon-links'; | ||
import {OrientationSensor, withOrientation} from '..'; | ||
import ShowDocs from '../../../.storybook/ShowDocs' | ||
|
||
const Print = ({orientation}) => | ||
<pre style={{fontFamily: 'monospace'}}> | ||
{JSON.stringify(orientation, null, 4)} | ||
</pre>; | ||
|
||
const PrintOrientation = withOrientation(Print); | ||
|
||
storiesOf('Sensors/OrientationSensor', module) | ||
.add('Documentation', () => h(ShowDocs, {name: 'OrientationSensor'})) | ||
.add('FaCC', () => | ||
<OrientationSensor>{(state) => | ||
<pre style={{fontFamily: 'monospace'}}>{JSON.stringify(state, null, 4)}</pre> | ||
}</OrientationSensor> | ||
) | ||
.add('HOC', () => <PrintOrientation />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {Component} from 'react'; | ||
import {SyncSensor} from '../SyncSensor'; | ||
import {h, on, off, isClient} from '../util'; | ||
|
||
export interface IOrientationSensorProps { | ||
children?: (state: IOrientationSensorState) => React.ReactElement<any>; | ||
} | ||
|
||
export interface IOrientationSensorState { | ||
angle: number; | ||
type: string; | ||
} | ||
|
||
const DEFAULT = { | ||
angle: 0, | ||
type: 'landscape-primary' | ||
}; | ||
|
||
const addListener = (handler) => on(window, 'orientationchange', handler); | ||
const removeListener = (handler) => off(window, 'orientationchange', handler); | ||
const onEvent = () => { | ||
const {orientation} = screen as any; | ||
|
||
if (!orientation) { | ||
return DEFAULT; | ||
} | ||
|
||
const { | ||
angle, | ||
type | ||
} = orientation; | ||
|
||
return { | ||
angle, | ||
type | ||
}; | ||
}; | ||
|
||
const getInitialState = () => { | ||
return isClient ? onEvent() : DEFAULT; | ||
}; | ||
|
||
export class OrientationSensor extends Component<IOrientationSensorProps, any> { | ||
initial = getInitialState(); | ||
|
||
render () { | ||
return h(SyncSensor, { | ||
children: this.props.children, | ||
initial: this.initial, | ||
addListener, | ||
removeListener, | ||
onEvent | ||
}); | ||
} | ||
} | ||
|
||
export const withOrientation = (Comp) => | ||
(props) => | ||
h(OrientationSensor, null, (orientation) => | ||
h(Comp, { | ||
...props, | ||
orientation | ||
}) | ||
); |
Oops, something went wrong.