Skip to content

Commit 9c4f6bd

Browse files
EMOTION
1 parent 596012c commit 9c4f6bd

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

react.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,4 +1593,69 @@ const StyleComponent = () => {
15931593
return (
15941594
<h1 style={styles}>Styled Title</h1>
15951595
);
1596-
};
1596+
};
1597+
1598+
1599+
// EMOTION
1600+
import React from 'react';
1601+
import styled from '@emotion/styled';
1602+
const Box = styled.div`
1603+
background-color: #ddd;
1604+
color: #444;
1605+
padding: 10px;
1606+
`;
1607+
const BoxComponent = () => {
1608+
return (
1609+
<div>
1610+
<h1>Box component</h1>
1611+
<Box>I'm a box!</Box>
1612+
</div>
1613+
);
1614+
};
1615+
export default BoxComponent;
1616+
1617+
1618+
// EMOTION EXTERNAL
1619+
import { forwardRef } from 'react';
1620+
import Styled from './Component.styled';
1621+
const Component = forwardRef((props, ref) => (
1622+
<Styled.Component ref={ref} {...props}>
1623+
Content of the <span>component</span>
1624+
<Styled.AsBox textColor="blue" />
1625+
<Styled.AsBox isRed />
1626+
</Styled.Component>
1627+
));
1628+
export default Component;
1629+
1630+
import styled from '@emotion/styled';
1631+
// styled system allows fast css with props -> m="10px" will return 'margin: 10px;'
1632+
import { layout, space, typography } from 'styled-system';
1633+
import { Box } from 'components';
1634+
export default {
1635+
Component: styled.p`
1636+
// a theme.js is adviced for consistency
1637+
${(p) => `
1638+
font-size: ${p.theme.fontSizes[2]};
1639+
text-align: center;
1640+
margin: ${p.theme.space[4]}px 0;
1641+
span {
1642+
color: ${p.theme.colors.red};
1643+
}
1644+
`}
1645+
${layout}
1646+
${space}
1647+
${typography}
1648+
`,
1649+
AsBox: styled(Box)`
1650+
// AsBox will take all CSS from Box component + those defined here
1651+
background: black;
1652+
// will apply the value passed to textColor prop
1653+
color: ${(p) => p.textColor};
1654+
// these styles will apply only if component has 'isRed' prop
1655+
${(p) =>
1656+
p.isRed &&
1657+
`
1658+
background: red;
1659+
`}
1660+
`
1661+
}

0 commit comments

Comments
 (0)