-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathDirectionProvider.jsx
More file actions
60 lines (50 loc) · 1.5 KB
/
DirectionProvider.jsx
File metadata and controls
60 lines (50 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This component provides a string to React context that is consumed by the
// withDirection higher-order component. We can use this to give access to
// the current layout direction for components to use.
import PropTypes from 'prop-types';
import React from 'react';
import { forbidExtraProps } from 'airbnb-prop-types';
import brcast from 'brcast';
import brcastShape from './proptypes/brcast';
import directionPropType from './proptypes/direction';
import { DIRECTIONS, CHANNEL } from './constants';
const propTypes = forbidExtraProps({
children: PropTypes.node.isRequired,
direction: directionPropType.isRequired,
inline: PropTypes.bool,
});
const defaultProps = {
inline: false,
};
const childContextTypes = {
[CHANNEL]: brcastShape,
};
export { DIRECTIONS };
export default class DirectionProvider extends React.Component {
constructor(props) {
super(props);
this.broadcast = brcast(props.direction);
}
getChildContext() {
return {
[CHANNEL]: this.broadcast,
};
}
componentDidUpdate(prevProps) {
if (prevProps.direction !== this.props.direction) {
this.broadcast.setState(this.props.direction);
}
}
render() {
const { children, direction, inline } = this.props;
const Tag = inline ? 'span' : 'div';
return (
<Tag dir={direction}>
{React.Children.only(children)}
</Tag>
);
}
}
DirectionProvider.propTypes = propTypes;
DirectionProvider.defaultProps = defaultProps;
DirectionProvider.childContextTypes = childContextTypes;