Анна Селезнёва, Spiral Scout
Анна Селезнёва
#freehugs
css
styles.css
js
scripts.js
index.html
components
Component
styles.css
scripts.js
components
Component
index.js
<button class="button">
Submit
</button>
<button class="armageddon">
Reset
</button>
<button class="button">
Submit
</button>
<button class="button button_red">
Reset
</button>
import styles from './style.css';
<button className={styles.button}>
Submit
</button>
<button className={`${styles.button} ${red ? styles.buttonRed : ''}`}>
Reset
</button>
<Button>
Submit
</Button>
<Button red>
Reset
</Button>
.button {
background: blue;
color: white;
}
.button_red {
background: red;
}
.button {
background: blue;
color: white;
&_red {
background: red;
}
}
const Button = 🎁`
background: blue;
color: white;
${props => props.red && css`
background: red;
`}
`;
by @okonet
import styled from 'styled-components';
styled(element)`` 🎉
const Button = styled('button')``
аналогично
const Button = ({ children }) =>
<button>{children}</button>
React.createElement(
elementToBeCreated,
propsForElement
);
styled('button')``;
аналогично
styled.button``;
tag`This ${component} has ${styles}`
аналогично
tag(['This ', ' has '], component, styles)
const RedButton = styled.button`
background: red;
color: white;
`;
<RedButton>Click</RedButton>
<button class="rDbtTn">Click</button>
<style data-styled-components="">
.rDbtTn {
background: red;
color: white;
}
</style>
<Button background="blue">
const Button = styled.button`
background: ${({ background }) => background};
color: white;
`;
const Button = styled.button`
background: blue;
color: white;
`;
const RedButton = styled(Button)`
background: red;
`;
import { Link } from 'react-router-dom';
const RedLink = styled(Link)`
color: red;
`;
const Button = styled.button.attrs(({ type }) => ({
type: type || 'button',
}))`
background: blue;
`;
<Button
as="a"
href="/next_page"
>
Skip
</Button>
import { Link } from 'react-router-dom';
<Button
as={Link}
to="/next_page"
>
Skip
</Button>
import { css } from 'styled-components';
const blueOrRedBackground = css`
background: ${props => props.red ? 'red' : 'blue'};
`;
/* или */
const blueOrRedBackground = props => css`
background: ${props.red ? 'red' : 'blue'};
`;
export const Button = styled.button`
${blueOrRedBackground};
`;
export const Square = styled.div`
width: 100px;
height: 100px;
${blueOrRedBackground};
`;
import styled, { keyframes } from 'styled-components';
const spin = keyframes`
to: { transform: rotate(360deg); }
`;
const Spinner = styled.div`
animation: ${spin} 1s linear infinite;
`;
import { createGlobalStyle } from 'styled-components';
const GlobalStyle = createGlobalStyle`
body {
color: red;
};
`;
<React.Fragment>
<GlobalStyle />
<App />
</React.Fragment>
export const theme = {
color: {
primary: 'blue',
success: 'green',
error: 'red',
}
};
import { ThemeProvider } from 'styled-components';
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>
const Button = styled.button`
background: ${({ theme }) => theme.color.success};
`;
<Button ref={ref => { this.buttonRef = ref }}>
Submit
</Button>
yarn add polished
Polished — это, по сути Lodash в мире CSS-в-JS
import { hideVisually } from 'polished';
const div = styled.div`
${hideVisually()};
`;
Результат:
{
border: 0;
clip: rect(0 0 0 0);
clipPath: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
whiteSpace: nowrap;
width: 1px;
}
radialGradient, ellipsis, timingFunctions, darken, lighten
и многое другое
import styledNormalize from 'styled-normalize';
/* или */
import styledSanitize from 'styled-sanitize';
const GlobalStyle = createGlobalStyle`
${styledNormalize}
...
`;
yarn add babel-plugin-styled-components
.babelrc
{
"plugins": ["babel-plugin-styled-components"]
}
<button class="Button-stLdCm stLdCm" />
вместо
<button class="stLdCm" />
env.production
"plugins": [
[
"babel-plugin-styled-components",
{ "displayName": false }
]
]
stylelint-processor-styled-components
stylelint-config-styled-components
stylelint-config-recommended
.stylelintrc
{
"processors": ["stylelint-processor-styled-components"],
"extends": [
"stylelint-config-recommended",
"stylelint-config-styled-components"
]
}
import 'jest-styled-components';
test('it works', () => {
const tree = renderer.create(<RedButton />).toJSON();
expect(tree).toHaveStyleRule('background', 'red');
});
export const marginProps = props => css`
${props.marginBottom && `margin-bottom: ${typeof (props.marginBottom) === "string" ? props.marginBottom : "1em"}`};
${props.marginTop && `margin-top: ${typeof (props.marginTop) === "string" ? props.marginTop : "1em"}`};
${props.marginLeft && `margin-left: ${typeof (props.marginLeft) === "string" ? props.marginLeft : "1em"}`};
${props.marginRight && `margin-right: ${typeof (props.marginRight) === "string" ? props.marginRight : "1em"}`};
${props.margin && `margin: ${typeof (props.margin) === "string" ? props.margin : "1em"}`};
${props.marginVertical && `
margin-top: ${typeof (props.marginVertical) === "string" ? props.marginVertical : "1em"}
margin-bottom: ${typeof (props.marginVertical) === "string" ? props.marginVertical : "1em"}
`};
${props.marginHorizontal && `
margin-left: ${typeof (props.marginHorizontal) === "string" ? props.marginHorizontal : "1em"}
margin-right: ${typeof (props.marginHorizontal) === "string" ? props.marginHorizontal : "1em"}
`};
`;
const Square = styled.div`
width: ${({ size }) => size}px;
height: ${({ size }) => size}px;
`;
<Square size="100"></Square>
<div size="100" class="..."></div>
const Square = styled(
({size, ...restProps}) => <div {...restProps} />
)`
width: ${({ size }) => size}px;
height: ${({ size }) => size}px;
`;
const Tag = styled.div`
font-size: ${({ size }) => size}px;
`;
<Tag size={/* anything */} />
<style data-styled-components="">
<Tag size={10} />
.fRstTg { font-size: 10px; }
<Tag size={12} />
.SCnDtG { font-size: 12px; }
<Tag size={24} />
.tHrDtG { font-size: 24px; }
</style>
Over 200 classes were generated for component styled.div
const Tag = styled.div.attrs({
style: ({ size }) => ({ fontSize: `${size}px` })
})`
...
`;
Из документации
Презентация: askd.rocks/pres/styled-gdg