Summary: My benchmarks have shown react-emotion to be faster than the current implementation of `styled`. For this reason, I am converting all styling to [emotion](https://emotion.sh). Benchmark results: {F136839093} The syntax is very similar between the two libraries. The main difference is that emotion only allows a single function for the whole style attribute, whereas the old implementation had functions for every style-attirbute. Before: ``` { color: props => props.color, fontSize: props => props.size, } ``` After: ``` props => ({ color: props.color, fontSize: props.size, }) ``` Reviewed By: jknoxville Differential Revision: D9479893 fbshipit-source-id: 2c39e4618f7e52ceacb67bbec8ae26114025723f
69 lines
1.4 KiB
JavaScript
69 lines
1.4 KiB
JavaScript
/**
|
|
* Copyright 2018-present Facebook.
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
* @format
|
|
*/
|
|
|
|
import {Component} from 'react';
|
|
import Box from './Box.js';
|
|
import {colors} from './colors';
|
|
import styled from '../styled/index.js';
|
|
|
|
const FocusableBoxBorder = styled(Box)({
|
|
border: `1px solid ${colors.highlight}`,
|
|
bottom: '0',
|
|
left: '0',
|
|
pointerEvents: 'none',
|
|
position: 'absolute',
|
|
right: '0',
|
|
top: '0',
|
|
});
|
|
|
|
export default class FocusableBox extends Component<
|
|
Object,
|
|
{|
|
|
focused: boolean,
|
|
|},
|
|
> {
|
|
constructor(props: Object, context: Object) {
|
|
super(props, context);
|
|
this.state = {focused: false};
|
|
}
|
|
|
|
static defaultProps = {
|
|
focusable: true,
|
|
};
|
|
|
|
onBlur = (e: SyntheticFocusEvent<>) => {
|
|
const {onBlur} = this.props;
|
|
if (onBlur) {
|
|
onBlur(e);
|
|
}
|
|
if (this.state.focused) {
|
|
this.setState({focused: false});
|
|
}
|
|
};
|
|
|
|
onFocus = (e: SyntheticFocusEvent<>) => {
|
|
const {onFocus} = this.props;
|
|
if (onFocus) {
|
|
onFocus(e);
|
|
}
|
|
if (this.props.focusable) {
|
|
this.setState({focused: true});
|
|
}
|
|
};
|
|
|
|
render() {
|
|
const {props} = this;
|
|
|
|
return (
|
|
<Box {...props} onFocus={this.onFocus} onBlur={this.onBlur} tabIndex="0">
|
|
{props.children}
|
|
{this.state.focused && <FocusableBoxBorder />}
|
|
</Box>
|
|
);
|
|
}
|
|
}
|