CSS in JS(Elm)ãããæ³å以ä¸ã«è¯ãã£ã
追è¨
ææ°ã®ææ³ãåããã¦ã覧ãã ããã
Reactçéã§ã¯çµæ§åãããCSS in JSãã¨è¨ã£ã¦ãéã«è¨ãã¨ãCSSã¯ã¤ã±ã¦ãªãããJSã§ã¤ã³ã©ã¤ã³ã¹ã¿ã¤ã«ãæ¸ãã¦ãã¾ããã¨ãã話ããããï¼ã¡ããã¨ç¥ããã人ã¯ãã¡ãï¼
èªåãåã ããCSSã¯å¤æ°ã使ããªãã¨ãååã被ãã¨ã諸ã ã¤ã±ã¦ãªãã®ã¯åæãã¦ããã ãã©ããããJSã§æ¸ãã®ãè¯ããã¨è¨ãããããããæµç³ã«ãã¸ãã¯æ±ããããããï¼ãã¨ããCSSã®ä¾¿å©æ©è½ãæ¨ã¦ã¦å¹³æ°ãªã®ï¼ãã¨ãè²ã ã¨æççã ã£ããã ãã©ãï¼ï½ï¼ãææ¸ãã¦ã¿ããæ³å以ä¸ã«è¯ãã£ãã®ã§ææ³ãæ¸ããã¨ã«ããã
ã¾ãä¸çªã«ä¸»å¼µãããé¨åãå ã«è¨ãã¨ãããã
ï¼èª¤è§£ï¼JSã®ã³ã¼ããã¹ã¿ã¤ã«è¨è¿°ã§æ±ãã
ï¼æ£è§£ï¼JSã®ã³ã¼ããã¹ã¿ã¤ã«è¨è¿°ãã解æ¾ããã
åæ
- å®éã«æ¸ããã®ã¯JavaScriptã§ã¯ãªãElmãªã®ã§ä»¥ä¸ã¯å ¨ã¦Elmã³ã¼ãã§æ¸ããã ãã©ãæ¬è³ªã¯å¤ãããªãã¯ããªã®ã§JavaScriptã«ç½®ãæãã¦èªãã§ãã ããã
- ãã¶ã¤ãã¨åæ¥ããããããã¯ã·ã§ã³ã§å®éã«ä½¿ã£ãããã§ã¯ãªãã®ã§ããã®è¾ºã®ãã¦ãã¦ã¯ãªãã§ãã
- ããã©ã¼ãã³ã¹ã®è©±ã¯ãªãã§ããé ããªã£ããèãããã¨æã£ããã©é ããªããªãã£ãã®ã§ã
- CSSã«å¯¾ããåé¡æèã¯ããç¨åº¦å ±æã§ãã¦ãã¦ãReactçãªæ¸ãæ¹ããã¦ããWebã¢ããªã対象ã
ã¨ããããã§ããããããææ³ã¨ããããã£ã¦ã¿ã¦åãã£ããã¨ã
ã¹ã¿ã¤ã«ã¯ä»ã¾ã§éãåé¢ã§ãã
ã¾ã誤解ããã¦ãããããããªãã¨ããã解æ¶ããããCSS in JSãã¨è¨ã£ã¦ããã¡ã¤ã³ã®ãã¸ãã¯ä¸ã«ãã¿ãã¿ã¹ã¿ã¤ã«ãè¨è¿°ãã¦ããããã§ã¯ãªãã¦ãã¹ã¿ã¤ã«ã¯æ®éã«åé¢ãã¦æ¸ãã¨ãããã¨ãåºæ¬çã«ã¯ä»ã¾ã§style.css
ã«æ¸ãã¦ããå
容ãstyle.js
ã«æ¸ãã ãã
Elmã§ã¯ãããªæãã§ãStyles.elmããè¨è¿°ãããé°å²æ°ãè¦ã¦ãããã®ãç®çãªã®ã§è©³ç´°ãªå 容ã¯è¿½ããªãã¦OKã
module Styles exposing (..) zIndex = { subView = "600" , messageBar = "700" , contextMenu = "800" } messageBar = [ ("position", "absolute") , ("color", "#fff") , ("z-index", zIndex.messageBar) , ("padding", "5px 10px") , ("transition", "height opacity 0.8s linear") ] successBar = messageBar ++ [ ("background-color", "#4c5") , ("opacity", "1") ] errorBar = messageBar ++ [ ("background-color", "#d45") , ("opacity", "1") ] noneBar = messageBar ++ [ ("opacity", "0") , ("pointer-events", "none") , ("background-color", "#4c5") ]
CSSãæ¸ãã¦ãã®ã¨ä½ãå¤ãããªãã使ç¨ããå´ã®ã³ã¼ãã¯ä»¥ä¸ã
successView msg = div [ style Styles.successBar ] [ text msg ] errorView message = div [ style Styles.errorBar ] [ text message ] noneView = div [ style Styles.noneBar ] [ ]
ä»ã¾ã§classå±æ§ã§ã¹ã¿ã¤ã«æå®ãã¦ããé¨åãstyleå±æ§ã«ç´ãã ããã¹ã¿ã¤ã«ã¯Style.elm
ã«æ¸ãã¦ããããã使ç¨ããå´ã®ã³ã¼ãã¯å
¨ãæ±ããªããä»ã¾ã§éãæ®éã«åé¢ã§ããã
便å©ãªCSSã¯ãã®ã¾ã¾ä½¿ãã
ãã¹ã¿ã¤ã«ãJSãã§ã¨èãã¨ãä»ã¾ã§ã®CSSã®ç¥è¦ããã¹ã¦æ¨ã¦ã¦ä½ããä½ã¾ã§èªåã§åº§æ¨è¨ç®ããä½ãããããªãããããªããããªæ°ããã¦ãããããããªãã¨ã¯å
¨ããªããä¸ã®ä¾ã«ç¤ºããããã«ãposition
ãz-index
ãpointer-events
ã®ãããªã»ã¼ãã¹ã¦ã®CSSã®ä¾¿å©ããããã£ã¯å¥å¨ã ãVirtual DOMã使ã£ã¦ãã¦ãtransition
ãåä½ããªããªã©ã®ãã°ãç¹ã«ç¡ãã£ãã
ã¹ã¿ã¤ã«ã®åæãå¯è½
ããã¯ãCSS in JSãã®ããããã®åæ©ãªã®ã§è¨ãã¾ã§ããªãã®ã ãã使ãã¾ãããã¹ã¿ã¤ã«ã¯å¤æ°ã«ãã¦ãããã¨ã§ç°¡åã«åæã§ãããä¸ã®ä¾ã§ããã¨ãsuccessBar
ã¯messageBar
ã«æåæç¹æã®ã¹ã¿ã¤ã«ï¼ç·è²ã¨ãï¼ã足ãåããããã¨ã§å®ç¾©ã§ããã
OOCSSï¼Bootstrapã®ãããªï¼ã§ãåæã¯åºæ¥ãã®ã ããå¿
è¦ãªclassãå
¨ã¦æ¸ãé£ããå¿
è¦ãããã®ã§HTMLãã©ãã©ãæ±ãã¦ãã£ã¦ãã¾ãï¼class="message-bar message-bar-success"
ï¼ãã¹ã¿ã¤ã«å®ç¾©å´ã§åæããã°HTMLã¯ã¯ãªã¼ã³ã«ä¿ã¦ããããHTMLã«ã¯ã»ãã³ãã£ã¯ã¹ãè¨è¿°ãã¦ã¹ã¿ã¤ã«ã¯CSSå´ã§ï½ãã¨ããæãªããã®ä¾¡å¤ãåãæ»ããã¨ãåºæ¥ãã
ã¹ã¿ã¤ã«ãé¢å¿å¥ã«ã¾ã¨ãã¦ç®¡ç
å¤æ°ã使ããã¨ã§ãããããã¹ã¿ã¤ã«ã®ç®¡çãå¯è½ã ãä¾ãã°ãè²ã¯å
¨é¨ã¾ã¨ãã¦ä¸ã¤ã®ãã¡ã¤ã«ã«å®ç¾©ãã¦ãããã¨ããz-index
ã¯ä¸ãæã«ã¾ã¨ãã¦ç®¡çãããã®ãããªãã¼ãºã«ãèªç±èªå¨ã«å¯¾å¿ã§ããã
åçãªå¤ã使ã£ãè¨ç®ãå¯è½
CSSï¼Sassãªã©ã®ããªããã»ããµãå«ãï¼ã«ã©ããã¦ãåºæ¥ãªããã¨ã®ä¸ã¤ã«ãå®è¡æã«åçã«å¤åããå¤ãåç §ãã¨ãããã®ãããããç»é¢ãµã¤ãºãããã®å ¸åä¾ã§ããã¡ã¤ã³ã³ã³ãã³ãã®é«ãã¯ç»é¢å ¨ä½ã®é«ããããããã®é«ããå¼ããå¤ãã¨ããã³ã³ãã¯ã¹ãã¡ãã¥ã¼ãç»é¢ããã¯ã¿åºãªãããã«ä½ç½®ã調æ´ãããã¿ãããªãã¨ãåºæ¥ãªããä»æ¹ããªãã®ã§ä»ã¾ã§ãããããã¸ãã¯ã¯ä¾å¤çã«JavaScriptå ã«æ¸ãã¦ããã®ã ãããCSS in JSãã§ã¯åçã«å¼æ°ãä¸ãããã¨ãåºæ¥ãã
以ä¸ã¯å¼æ°ãç¨ããã¹ã¿ã¤ã«å®ç¾©ã®ä¾ï¼Elmï¼ã
-- ç»é¢ã®é«ããå¼æ°ã«åã mainView windowHeight = ( flex ++ [ ("height", px (windowHeight - headerHeight)) , ("position", "relative") ] )
æ°å¤ã ãã§ã¯ãªãããã©ã°çãªãã®ãçªã£è¾¼ããã¨ãã§ãããã¿ããªã©ãé¸æããã¨ãã®active
ç¶æ
ãªã©ãå
¸åä¾ãå®éã®ã³ã¼ãããã®æç²ã
-- æ°å¤ï¼ã¤ã³ããã¯ã¹ï¼ã¨çå½å¤ã®ï¼ã¤ãå¼æ°ã«åã subViewTab index active = [ ("position", "absolute") , ("top", px (10 + index * 130)) , ("left", "-30px") , ("width", "30px") , ("height", "120px") , ("padding-left", "6px") , ("line-height", "135px") , ("background-color", "#eee") , ("z-index", zIndex.subView) , ("cursor", "pointer") , ("border-radius", "8px 0 0 8px") , ("box-shadow", if active then "" else "inset -4px 0 4px rgba(0,0,0,0.03)") , ("box-sizing", "border-box") ]
ä»ã¾ã§ã¯ãããåºæ¥ãªãã£ãã®ã§HTMLãJavaScriptã«æ¸ããã¦ããããæ¬æ¥ãã®ãããªè¨è¿°ã¯æ®éã«CSSã«æ¸ãããã¹ããã®ã®ã¯ãã ããã¡ããä½ã§ãããã§ããã¸ãã¯ãçªã£è¾¼ãã§ãã¾ãã¨è²¬åããªã¼ãã¼ãã¦ãã¾ãã®ã§ãããã¯ç¯åº¦ãå®ã£ã¦ããã
CSSã使ã£ãæ¹ãè¯ãå ´é¢ããã
ãCSS in JSãã ããã¨è¨ã£ã¦ãå¥ã«CSSã使ããã¨ãç¦æ¢ããçç±ããªãã®ã§å¿ è¦ãªãCSSã使ãã
ã¾ããã¤ã³ã©ã¤ã³ã¹ã¿ã¤ã«ã§é£ãããã¨ã¨ãã¦æåã«æãæµ®ãã¶ã®ã:hover
ãæµç³ã«ããã¯CSSã§ãããªãã¨é¢åã ãããJavaScriptã§ãåºæ¥ãªãã¯ãªãããenter
/leave
ã¤ãã³ããèªåã§ãã³ãã«ããå¿
è¦ãããããããããã®ããã¼ãã¦ããç¶æ
ããã¸ãã¯ã¨ãã¦å¿
è¦ã«ãªãã±ã¼ã¹ããããã¨è¨ã£ãããã¾ããªããã ãã:hover
ã¯è«¦ãã¦æ®éã«classå±æ§ã使ããã¨ã«ããã
次ã«ãresetç³»CSSããããCSSã§ãã£ã¦ãããªãã¨é¢åãªãã¨ã«ãªãããããªãå ´åãå ¨ã¦ã®ã¹ã¿ã¤ã«ã¯ãã®ãã¼ã¹ã®ã¹ã¿ã¤ã«ãç¶æ¿ãã¦ãã ãããã®ãããªåä»ãªã«ã¼ã«ãä½ããã¨ã«ãªããå«ãªäºæããããªãã®ã§æåããé¿ããã®ãè¯ãããã ã
å¼±ç¹ã«è¦ããé¨åãã»ã¼ä»£æ¿æ段ããã
ãã®ä»ãæ°ä»ããç¹ã
animation
ã¯ãã¾ãåºæ¥ãªããJavaScriptã§ãããâ»æªæ¤è¨¼::before
/::after
çä¼¼è¦ç´ ã¯æ®éã«JavaScript/HTMLã«æ¸ãã°åé¡ãªãã- ã¡ãã£ã¢ã¯ã¨ãªã¯ãããããã©ã°ã§åºæ¥ããâ»æªæ¤è¨¼
- ãã©ã¦ã¶ã®ãã¼ã«ã§åãã¹ã¿ã¤ã«ãä¸æ¬å¤æ´ãã¦ãã¬ãã¥ã¼ã§ããªããä¾ãã°ã
<li>
è¦ç´ ã®æåã®ä¸ã¤ããã©ã¦ã¶ä¸ã§å¤æ´ãã¦ãæ®ãã«ã¯åæ ãããªããJavaScriptã®ã¹ã¿ã¤ã«å®ç¾©ãæ¸ãç´ãã¦ç»é¢ããªãã¬ãã·ã¥ããå¿ è¦ãããã - Bootstrapã®ãããªCSSãã¬ã¼ã ã¯ã¼ã¯ã使ããªããããã®åé¡ã解決ãããã®ã§ä»æ¹ããªãããCSS in JSãç¨ã®è¯ããã¬ã¼ã ã¯ã¼ã¯ãåºã¦ããã®ãå¾ ã¤ããèªåã§ä½ãã
èªåãç´å¾ã®ã§ããã¡ãªãããè¦ã¤ãã
ãCSSã®å¼±ç¹ãã¨ãã§èª¿ã¹ãã¨ã¢ã¸ã¥ã©ãªãã£ãã©ãã¨ããã話ãããåºã¦ããã®ããã¶ã£ã¡ããããåãããªãã£ããå ·ä½çã«ã©ãããã³ã¼ãã ã¨ã¢ã¸ã¥ã©ãªãã£ãä¸ãã£ãç¶æ ã§ä½ã解決ããã®ãããåãããªãã
ã ãããz-indexãã¼ãã§ããBEMãããã©ããããã§ããæ¸ãã¦ã¦å¿«æç©è³ªãåºããã§ããªãã§ããããã©ãçå±ã¯ãã¦ããèªåã®å®æã§ããçç±ã§æ¡ç¨ããã°è¯ãããããªãããªã¨ãéã«å®æã§ããªããªã主張ãã¦ãããã¾ãæå³ããªãããã¨ãã°åå空éã®åé¡ãçå±ä¸ãããªãã ãããã©ãèªåãããã§å°ãããã¸ã§ã¯ãã«é¢ãã£ã¦ãªãã¦ããåãããªãã£ãã®ã§ãããã«ã¯æ¸ããªãã£ãããã¨ãCSSã§ã¯ãªãã¦JSã®ã¢ã¸ã¥ã¼ã«ã¨ãã¦æ¸ããã®ã§ã¨ã³ã·ã¹ãã çã«ãè¯ãã¨æã£ã¦ãã¦ãæ¬å½ã¯ãããè¨ããããã ãã©è©¦ãæ©ä¼ããªãã£ãã®ã§èª°ãç¥è¦ãæã£ã¦ããã°èãããã
ã¾ã¨ã
æåã«æãããã¹ã¿ã¤ã«ãåé¢ã§ããªããªãããããªãããã¨ããæ¸å¿µã¨ã¯çéã§ããããä»ã¾ã§JSã§ã´ãªã´ãªæ¸ãã¦ããã¹ã¿ã¤ã«ã«é¢ãããã¸ãã¯ããã¡ããã¨ããã¹ãå ´æã«åã¾ã£ãã
ã¬ãããã©ã¤ã
èªåèªèº«ã®åçã§ããããã ãã©ãåºå®è¦³å¿µã¯è¯ããªãã¨æãã¾ããã
以ä¸ã