JX é信社ã®ããã³ãã¨ã³ãã§ã¯ React TypeScript ã Emotion ã®ãã㪠CSS in JS ãæè¡é¸å®ãããã¨ãå¤ãã§ããå¼ç¤¾ SaaS ã® FASTALERTãæ°åã³ããé¢é£æ å ±ãªã©ã§ãåæ§ã®æè¡é¸å®ã§ãéå»ã«ãã¨ã³ã¸ãã¢ããã°ã§ç´¹ä»ãã¦ãã¾ããã
ä»æ¥ã¯ãEmotion ã®æ´»ç¨ã®æ¥µå°ãUtility First 㪠CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ãã«ã¤ãã¦ãç´¹ä»ãã¾ãã
Emotion ã§éçºããæ©ã¿
ç´ ã® Emotion ãé¡ä¼¼ã® CSS in JS ã©ã¤ãã©ãªã§ã¯ã 1 ã¤ã® TS/JS ãã¡ã¤ã«å
ã« CSS ãæ¸ããããªæãã§ã¹ã¿ã¤ã«è¨å®ãè¡ã£ã¦ããã¾ã*1ãCSS in JS ã©ã¤ãã©ãªã«æ¦ãå
±éãã¦ããã®ãã styled.ã¿ã°å
ã§ã¹ã¿ã¤ãªã³ã°ãããã¨ã§ãã
const Title = styled.h1` font-weight: bold; font-size: 20px; ` const Entry = styled.div` line-height: 1.5; margin-top: 20px; ` type Props = { title: React.ReactNode } const Article: React.FC<Props> = ({ title, children }) => { return <> <Title>{title}</Title> <Entry>{children}</Entry> </> }
ã³ã³ãã¼ãã³ãå¿åã§ä½ã£ã¦ããä¸ã§ãä¸è¨ã®ã³ã¼ãã«ã¯è²¬åçãªåé¡ãããã¾ãã Entry
ã¯ãä¸é¨ã®è¦ç´ 㨠20px 空ãããã¨ãããã¼ã¸ã³æå®ããã¦ãã¾ããããé£æ¥è¦ç´ ã¨ã®è·é¢ãã¯å½è©²ã³ã³ãã¼ãã³ãã®è²¬åå¤ã§ããããã¾ã§ Entry
ãå¼ã³åºãå´(親)ããã©ããããã®éãéããã®ãé©åãç¥ã£ã¦ããã¹ãã§ãã
Utility First 㪠CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ãå°å ¥ããã¨ã次ã®ããã«æ¸ããã¨ãã§ãã¾ãã以ä¸ã®ä¾ã§ã®ãmtãã¨ãã prop ã¯ãmargin-top ãæå®ããããã® prop ã§ãã
import { Title, Entry } from '~/components' const Article: React.FC<Props> = ({ title, children }) => { return <> <Title>{title}</Title> <Entry mt={4}>{children}</Entry> </> }
ãã¼ã¸ã³ããµã¤ãºãè²ãªã©ã®ããããã¹ã¿ã¤ã«ã prop ã§æ¸¡ãããã㪠CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ããæ¬ç¨¿ã§ã¯ãUtility First 㪠CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ãã¨å¼ç§°ãã¾ãããã®ãããªãã¬ã¼ã ã¯ã¼ã¯ã¯ãç´ ã® Emotion ã styled-components ãæ¡å¼µãããããªå½¢(ä¸ç·ã«ã¤ã³ã¹ãã¼ã«ãã)ã§ä½ããã¦ãã¾ãã
Utility First 㪠CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ã®ã¡ãªãã
ä¸è¨ã®ä¾ã¯ inline style (style prop ã«ãããå½¢)ã§ãå®ç¾ã§ããä¾ã§ãããUtility First ãªãã¬ã¼ã ã¯ã¼ã¯ã«ã¯æ¬¡ã®ãããªã¡ãªãããããã¾ã*2ã
- åå®å ¨æ§ãé«ã
- ã¬ã¹ãã³ã·ãã«å¯¾å¿ãã¦ãã
- ãã¶ã¤ã³ã·ã¹ãã ã¨ã®è¦ªåæ§ãé«ã
- ããã¼æãªã©ã®æåãæå®ã§ãã
ä¾ãã°ã以ä¸ã®ã³ã¼ãã¯ãä¸è¨ã®ã¡ãªãããåæã«æºããã¦ãã¾ãã
import { x } from '@xstyled/emotion' const Box = (props) => { return <x.div // primary ã®ãããªç¬èªå®ç¾©ã®è²åãåå®å ¨ã« color="primary" // ã¬ã¹ãã³ã·ãæå® display={{ _: "block", md: "flex"}} // ââpx ãããªãããã¶ã¤ã³ã·ã¹ãã ã«åã£ããã¼ã¸ã³ãã¿ã¼ã³ãæå® mt={4} // ããã¼æã®è²æå® hoverColor="red" {...props} /> }
å®éã«æ¸ãã¦ã¿ãã¨ãããã®ã§ããã TypeScript (JavaScript)ã®ä»çµã¿ã®ä¸ã§åãã¦ããããã³ã¼ãè£å®ãå¹ãããããã³ã¼ãéãå°ãªãã¹ã¿ã¤ã«ãæ¸ããã®ã§ãçç£æ§ãé«ãæãããã¾ãããã®ãããèªåãé¢ããããã¸ã§ã¯ãã§ã¯ç©æ¥µçã«å°å ¥ãã¦ãã¾ãã
ããããã¯ããã®ãããªç¹è²ãæã£ã React åãã® CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ãï¼ã¤ç´¹ä»ãã¦ããã¾ãããããã®ãã¬ã¼ã ã¯ã¼ã¯ãã2019å¹´ããã«ãªãªã¼ã¹ãããã¢ã¯ãã£ãã«æ´æ°ããã¦ãã¾ããç¹ã«æè¿ã¯ Chakra UI ã®äººæ°ãé«ãããã§ãã2021 å¹´ 4 æç¾å¨ã® GitHub Stars ãã©ã¤ãã©ãªåã«ä½µè¨ãã¦ãã¾ãã
Chakra UI (â 16.6k)
Chakra UI 㯠Utility First 㪠CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ã¨ãã¦ã®æ©è½ã¨ã(Material UI ã®ãããª)ãªãããªæ¢æã³ã³ãã¼ãã³ããä¸ç·ã«ãªã£ãã©ã¤ãã©ãªã§ãã <Box>
ãåºæ¬çãªã¦ã¼ãã£ãªãã£ã³ã³ãã¼ãã³ãã«ãªã£ã¦ããã®ã§ãããã使ã£ã¦ããã¾ãã<Flex>
ã <Grid>
ã¨ãã£ãã¬ã¤ã¢ã¦ãç¨ã® Box ãç¨æããã¦ãã¾ãã
import { Box } from "@chakra-ui/react" const Entry = (props) => { return <Box // as 㧠div 以å¤ã®ã¿ã°ãæå®ã§ãã as="section" // ã¬ã¹ãã³ã·ã㯠{ base: 2, md: 3 } ã®ãããªå½¢ã ãã§ãªããarray ã§æå®ã§ãã m={[2, 3]} color="gray.600" {...props} />
xstyled (â 1.6k)
xstyled ã Chakra UI ã¨åããããªã¦ã¼ãã£ãªã㣠prop ãå®ç¾©ããã¦ãã¾ãããCSS in JS ãã¬ã¼ã ã¯ã¼ã¯ã¨ãã¦ã®æ©è½ã«ç¹åãã¦ãã¾ããxstyled 㯠<x.ã¿ã°å>
ã¨ããã³ã³ãã¼ãã³ããç¨æããã¦ãã¦ãé常ã®ã¿ã°ã§ã®ã³ã¼ãã£ã³ã°ã¨æ¸ãå³ãä¼¼ã¦ãã¾ãã
import { x } from "@xstyled/emotion" const Entry = (props) => { return <x.section // ã¬ã¹ãã³ã·ã m={{ _: 2, md: 3 }} // color="primary" {...props} />
4æ1æ¥ã«ãªãªã¼ã¹ãããä¿¡æ¿æ¯æ¥æ°èæ§ã¨ã®åé¢é·éè£é¸ç¹è¨ãã¼ã¸ ã§ã使ã£ã¦ãã¾ãã
Theme UI (â 3.6k)
Theme UI ã Chakra UI åæ§ã CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ã¨ãã¦ã®æ©è½ã¨ãä¸é¨æ¢æã³ã³ãã¼ãã³ããä¸ç·ã«ãªã£ãã©ã¤ãã©ãªã§ããChakra UI ã«æ¯ã¹ã¦ãæ¢æã³ã³ãã¼ãã³ãã¯å°ãªããã¾ããprop ã§ç¶²ç¾
çã« CSS æå®ã§ãããã¨ãå¿åãã¦ãã¾ãã*3ããã®ãã <Box>
ã ãã§ãªã sx
ã¨ãã prop (style prop ã«é¡ä¼¼)ãä½µç¨ãã¦ä½¿ãã¾ãã
import { Box } from "@xstyled/emotion" const Entry = (props) => { return <Box m={[2, 3]} sx={{ // textAlign ã¨ãã prop 㯠Box ã«åå¨ããªã textAlign: 'center' }} {...props} />
çªå¤ç·¨ï¼ï¼Tailwind CSS
æè¿ãã£ã¨ãããèã Utility First 㪠CSS ãã¬ã¼ã ã¯ã¼ã¯ã¯ Tailwind CSS ã§ããããTailwind ã¯ã¹ã¿ã¤ã«ãã¯ã©ã¹åã§æå®ãã¦ããã¾ããTailwind ã¯ãããã CSS-in-JS ã®ã©ã¤ãã©ãªã§ã¯ãªãã®ã§ãçªå¤ç·¨ã¨ãã¾ãããChakra UI ã xstyled ã¨ä¼¼ããããªæ©è½ãçãã¦ãã¾ãããåå®å ¨ãª prop ã§æ¸¡ãããã§ã¯ããã¾ããã
const Box = (props) => { return <div className="mt-4 rounded text-blue-600 md:text-green-600" {...props} /> }
çªå¤ç·¨ï¼ï¼èªåã§æ´ãã
æ¬ç¨¿ã§ç´¹ä»ãããããªã©ã¤ãã©ãªãå ¥ããããªãã¨ãã人ãããã¨æãã¾ãããããã®ã©ã¤ãã©ãªã¯ emotion ã styled-components ã®ä¸ã«ä½ããã¦ãããã¬ã¼ã ã¯ã¼ã¯ã«éããªããããèªåã§æ´ãã¦ãããã¨ãå¯è½ã§ãã
// mixins.ts type MarginProps = { m?: CSSProperties['margin'] } const marginMixin = ({ m }: MarginProps) => ({ margin: m, }) // Entry.tsx const Entry = styled.div<MarginProps>` ${marginMixin} line-height: 1.5; ` // Page.tsx const Page = () => { return ( <> <Title /> <Entry m={30} /> </> ) }
èªåã§ããã°è»½éã«å§ããããã®ã§ãããæåã«ç´¹ä»ãããããªã¡ãªãã(ã¬ã¹ãã³ã·ãç)ãç¶²ç¾ çã«ç¨æãã¦ããã®ãç¸å½é¢åãããã§ããstyled-system ã®ãããªã©ã¤ãã©ãªãããã¾ãããã¡ã³ãããã¦ãã¾ããã*4
IE 対å¿
Internet Explorer ã®ãµãã¼ããæ¨ã¦ãããªããã¨ããå ´åãããã¨æãã¾ãã対å¿ç¶æ³ãç°¡åã«ã¾ã¨ããã®ã§ããã²åèã«ãã¦ãã ããã
ã©ã¤ãã©ãª | å ¬å¼ãµãã¼ãã®å®£è¨ | åä½ | è£è¶³ |
---|---|---|---|
Chakra UI | â | â | |
xstyled | â | â | ããä¸é¨ã« CSS Variables ã使ããã¦ããã®ã§ããããé¿ããã°å¯è½*5 |
Theme UI | â³ | â | å
¬å¼ãµãã¼ãã®è¨è¼ã¯ãªãããCSS Variables ã使ããªãããã®è¨å®ãæ示ããã¦ããcore-js/web/dom-collections ã®ããªãã£ã«ãå¿
è¦ |
Tailwind | â | æªèª¿æ» |
æ®å¿µãªãããæ確ã«å ¬å¼ãµãã¼ãã謳ã£ã¦ããã©ã¤ãã©ãªã¯ãªãã®ã§ããã xstyledãTheme UI ãããã¯åä½å¯è½ã§ããå ã»ã©ç´¹ä»ããåé¢é·éè£é¸ã®ç¹è¨ãµã¤ã ã§ã¯ IE 11 ããµãã¼ããã¦ãã¾ãããæ¦ãåé¡ãªãåãã¦ãã¾ãã
IE ã§å ¨ãåããªããã®ã¯ãCSS Variables ã使ã£ã¦ãããã®ã§ããCSS Variables ã®æ¹ãããã©ã¼ãã³ã¹ã¯ãããããªã®ã§ããã©ã¦ã¶äºææ§ã¨ããã©ã¼ãã³ã¹ã¨ã®ãã¬ã¼ããªãã«ãªãããã§ãã
ã¾ã¨ã
æ¬ç¨¿ã§ã¯ãUtility FIrst 㪠CSS in JS ãã¬ã¼ã ã¯ã¼ã¯ãç´¹ä»ãã¾ããããã¶ã¤ã³ã·ã¹ãã ã TypeScript ã¨ã®è¦ªåæ§ãé«ããçç£æ§é«ãã³ã¼ãã£ã³ã°ã§ããã®ã§ããã²å©ç¨ãæ¤è¨ãã¦ã¿ã¦ãã ããã
ã¾ããJX é信社ã§ã¯ããã³ãã¨ã³ãã«éããã¤ã³ã¿ã¼ã³çãéå¹´ã§åéãã¦ãã¾ãããã«ãªã¢ã¼ãã§ãåãã¾ãã
*1:ã¨ãã£ã¿ã®ãã©ã°ã¤ã³ãå ¥ããã¨ãã¹ã¿ã¤ã«é¨ã¯ CSS ã®ããã«ãã¤ã©ã¤ãããã¾ã
*2:xstyled ã®èª¬æãå ã«å çãã¦ãã¾ã
*3:ä¸ä½äºæã¨ããããã¯ãææ³ã®éãã ã¨æãã¾ã
*4:styled-system ã®ä½è ã Theme UI ãéçºã¡ã³ãã¼ã®ä¸äººã§ã
*5:ã½ã¼ã¹ã³ã¼ãã var ã§æ¤ç´¢ããã¨ã§ã¦ãã¾ãããFlex åã spacing ã transform ãªã©ã§ããè²ããµã¤ãºããã¼ã¸ã³ãªã©ã®åºæ¬æ©è½ã¯åé¡ãªãåãã¾ãã