Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Shadow DOMで CSSの悪夢に立ち向かう / WebComponents.kyoto ...
Search
Hidetaka Okamoto
March 06, 2018
Technology
0
220
Shadow DOMで CSSの悪夢に立ち向かう / WebComponents.kyoto vol.2
Hidetaka Okamoto
March 06, 2018
Tweet
Share
More Decks by Hidetaka Okamoto
See All by Hidetaka Okamoto
OpenAI APIで API Changelogを要約してみた話 / chatgpt-osaka-1
hideokamoto
0
460
コミュニティ運営から 中の人に変わって感じたこと
hideokamoto
0
53
Developerが Developer Advocateになった話 / dev-rel-meetup-tokyo-71
hideokamoto
0
270
Jamstack開発者のための App Runner入門
hideokamoto
1
420
WordPressでの webサイト制作2022 / ngk2022s
hideokamoto
0
380
JavaScript(TypeScript)で メディアサイトを インフラから構築する方法 / jsconf-jp-2021
hideokamoto
2
4k
AWS上でStripeを利用したアプリをより安全にデプロイする方法 /jaws-pankration-2021
hideokamoto
1
170
Shifter Headlessと Headless WordPressの紹介
hideokamoto
0
1.6k
Stripe & Next.js + AWS Amplify で会員 + 定期課金機能 / JP_Stripes20210903
hideokamoto
7
3k
Other Decks in Technology
See All in Technology
安心してください、日本語使えますよ―Ubuntu日本語Remix提供休止に寄せて― 2024-11-17
nobutomurata
1
990
Platform Engineering for Software Developers and Architects
syntasso
1
520
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
380
社内で最大の技術的負債のリファクタリングに取り組んだお話し
kidooonn
1
550
AIチャットボット開発への生成AI活用
ryomrt
0
170
Amazon CloudWatch Network Monitor のススメ
yuki_ink
1
200
SREによる隣接領域への越境とその先の信頼性
shonansurvivors
2
520
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.7k
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.4k
VideoMamba: State Space Model for Efficient Video Understanding
chou500
0
190
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
780
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
65
4.4k
Raft: Consensus for Rubyists
vanstee
136
6.6k
GraphQLとの向き合い方2022年版
quramy
43
13k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Visualization
eitanlees
145
15k
Designing for humans not robots
tammielis
250
25k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
GitHub's CSS Performance
jonrohan
1030
460k
A designer walks into a library…
pauljervisheath
204
24k
The Invisible Side of Design
smashingmag
298
50k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Transcript
Hidetaka Okamoto Shadow DOMͰ CSSͷѱເʹཱ͔ͪ͏ WebComponents.kyoto Meetup #2
Who are you ? Okamoto Hidetaka ❖ Digitalcube Co. Ltd.
❖ WordCamp Kyoto 2017࣮ߦҕһ ❖ JAWS-UG Kyoto / Kobe ❖ React.kyoto
Agenda ❖ CSSͷ໋໊نଇͷ ❖ ৽໋໊͍͠نଇͷରԠํ๏ ❖ αϯϓϧίʔυ ❖ ͦͷઌʹ͋Δ৽͍͠ѱເ
Agenda ❖ CSSͷ໋໊نଇͷ ❖ ৽໋໊͍͠نଇͷରԠํ๏ ❖ αϯϓϧίʔυ ❖ ͦͷઌʹ͋Δ৽͍͠ѱເ
CSS is global ❖ Կ͕άϩʔόϧʁ ❖ ɹ ❖ ɹ ❖
ɹ
CSS is global ❖ Կ͕άϩʔόϧʁ ɹˠɹ໊લۭ͕ؒάϩʔόϧ ❖ Ϋϥε໊͕ॏෳ͢Δͱ্ॻ͖͞ΕΔ ❖ ελΠϧͷઃఆΛܧঝͰ͖ͳ͍
❖ Ͳ͏ʹ͔ܧঝͰ͖ΔΑ͏ʹ͍ͨ͠ → ϓϦϓϩηοα ❖ ࠶ར༻ੑͷ͋ΔCSSΛॻ͖͍ͨ → ໋໊نଇ
CSS ϓϦϓϩηοα ❖ SassLess / PostCSSͳͲ ❖ CSSͰܧঝมͷར༻ͳͲΛͰ͖ΔΑ͏ʹ͢Δ ❖ ຊདྷͳΒͦͷ··͑ͳ͍ॻ͖ํʹͳΔ
❖ Ruby (Sass)Node.js (PostCSS)Ͱͷม͕ඞཁ
໋໊نଇ ❖ CSSΛద༻͢ΔΫϥε໊ͷ໋໊ϧʔϧ ❖ OOCSS / BEM / SMACSSͳͲ ❖
ػೳੑ࣭ʹ߹Θ໊ͤͨલΛͦΕͧΕ͚͍ͭͯ͘ ❖ CSSϓϦϓϩηοαͱΈ߹ΘͤΔͷ͕σϑΝΫτ
Α͋͘Δέʔε <div class="panel bordered wide”> OOCSSͰॻ͍ͨίʔυ </div> <style> .panel {
padding: 20px; } .bordered { border: 1px solid #fff; } .wide { max-width: 100%; } </style>
Α͋͘Δέʔε <div class="outer"> <div class="boilerplate">Hi! My name is</div> <div class="name">Bob</div>
</div> <style> .outer {} .boilerplate {} .name {} </style>
ΊΜͲ͍
Զͨͩɺ ͜ͷจࣈΛ͘ ͍͚ͨͩ͠ͳͷ ʹɾɾɾ
Agenda ❖ CSSͷ໋໊نଇͷ ❖ ৽໋໊͍͠نଇͷରԠํ๏ ❖ αϯϓϧίʔυ ❖ ͦͷઌʹ͋Δ৽͍͠ѱເ
ͳͥCSS͘͜͠ͳΔʁ ❖ Өڹൣғ͕άϩʔόϧʢશମΛߟྀ͢Δඞཁ͕͋Δʣ ❖ ܧঝ͕Ͱ͖ͳ͍ʢॏෳͷେྔൃੜʣ ❖ ελΠϧͷͨΊͷdiv͕େྔൃੜ͢Δ
͡Ό͋͜ΕΒΛͳ͍͍ͤ͘ ❖ Өڹൣғ͕άϩʔόϧʢશମΛߟྀ͢Δඞཁ͕͋Δʣ ❖ ܧঝ͕Ͱ͖ͳ͍ʢॏෳͷେྔൃੜʣ ❖ ελΠϧͷͨΊͷdiv͕େྔൃੜ͢Δ
https://www.w3.org/TR/shadow-dom/ Shadow DOM ͋ͨΒ͍͠HTMLͷ̍ཁૉ
Shadow DOMͰͳʹ͕͔ΘΔʁ ❖ ӨڹൣғΛߜΓࠐΉʢશମΛߟྀ͠ͳͯ͘ࡁΉʣ ❖ Custom ElementͰܧঝՄೳʢDRYʣ ❖ ελΠϧͷͨΊͷϚʔΫΞοϓΛӅಗ͢Δ
Agenda ❖ CSSͷ໋໊نଇͷ ❖ ৽໋໊͍͠نଇͷରԠํ๏ ❖ αϯϓϧίʔυ ❖ ͦͷઌʹ͋Δ৽͍͠ѱເ
͘ͳΔͷͲͬͪʁ <div id=“root”><h3>Light DOM</h3></div> <section><h3>nomal dom</h3></section> <script> let root =
document.querySelector('#root').createShadowRoot(); root.innerHTML = '<style>h3{ color: red; }</style>' + '<h3>Shadow DOM</h3>'; </script>
ਖ਼ղ <div id=“root”><h3>Light DOM</h3></div> <section><h3>nomal dom</h3></section> <script> let root =
document.querySelector('#root').createShadowRoot(); root.innerHTML = '<style>h3{ color: red; }</style>' + '<h3>Shadow DOM</h3>'; </script>
Shadow DOMͷத͚ͩ DOM͝ͱஔ͖͑ΒΕ͍ͯΔ
<content>Λ͔ͭ͏ͱ <div id=“root”><h3>Light DOM</h3></div> <section><h3>nomal dom</h3></section> <script> let root =
document.querySelector('#root').createShadowRoot(); root.innerHTML = '<style>h3{ color: red; }</style>' + ‘<content/>'; </script>
ݩͷHTML͕ग़Δ ͨͩ͠CSS͕ ͋ͨΒͳ͘ͳͬͨ
ϗετཁૉͳͷͰɺ:hostΛ͏ <div id=“root”><h3>Light DOM</h3></div> <section><h3>nomal dom</h3></section> <script> let root =
document.querySelector('#root').createShadowRoot(); root.innerHTML = ‘<style>:host { color: red; }</style>' + ‘<content/>'; </script>
ݩͷHTMLʹCSSΛ :hostͰ ϗετཁૉʹCSS͕͚ͭΕΔ
ͨͩ͠ϗετશମʹ :host h3ͷΑ͏ʹͰ͖ͳ͍
selectͰ໌ࣔతʹ <div id=“root”><h3>Light DOM</h3></div> <section><h3>nomal dom</h3></section> <script> let root =
document.querySelector('#root').createShadowRoot(); root.innerHTML = ‘<style>content[select="h3"]::content > h3 { color: red; }</style>' + ‘<div><content select="h3"></content><content select="p"/></div>'; </script>
͜ΕͰh3͚ͩ৭ʹ :hostͰ ϗετཁૉʹCSS͕͚ͭΕΔ
͕࣌ؒ͋ΕσϞΛ͢Δ
Agenda ❖ CSSͷ໋໊نଇͷ ❖ ৽໋໊͍͠نଇͷରԠํ๏ ❖ αϯϓϧίʔυ ❖ ͦͷઌʹ͋Δ৽͍͠ѱເ
Shadow DOMͰղܾ͢Δ͜ͱ ❖ CSSͷมߋʹΑΔӨڹൣғΛߜΓࠐΉ͜ͱ͕Ͱ͖Δ ❖ ০ͷͨΊͷHTMLΛॻ͔ͳͯ͘ࡁΉ ❖ CSSͷ໋໊نଇΛ͋·Γҙࣝ͠ͳͯ͘ॻ͚Δ
Shadow DOMͰղܾ͢Δ͜ͱ ❖ CSSͷมߋʹΑΔӨڹൣғΛߜΓࠐΉ͜ͱ͕Ͱ͖Δ ❖ ০ͷͨΊͷHTMLΛॻ͔ͳͯ͘ࡁΉ ❖ CSSͷ໋໊نଇΛ͋·Γҙࣝ͠ͳͯ͘ॻ͚Δ
άϩʔόϧͳͷCSS͚ͩʁ
<h1> HTMLλάͷ໊લ<br/> άϩʔόϧ </h1>
͓ͦΒ͘ى͖Δ͜ͱ <div> <x-my-special-content>͜Εͱ</x-my-special-content> <x-some-special-cnt>͜Ε</x-some-special-cnt> <another-element>͕͍ͪͳʹʁ</another-element> </div>
Shadow DOMͷͦͷઌ ❖ CSSͷ໋໊نଇ͋·Γҙࣝ͠ͳͯ͘Α͘ͳΔ ❖ ͨͩ͠HTMLλά(Custom Element)ͰϋϚΔ ❖ HTMLλάͷ໋໊نଇ͕͓ͦΒ͘ཚཱ͢Δ ❖
ͨͩ͠ܧঝ͕Ͱ͖ΔɺCSSΑΓѻ͍͍ͣ͢
༨ܭͳϚʔΫΞοϓͳ͠Ͱɺ ࣗͷ࡞Γ͍ͨViewΛ࡞Γग़͢ <name-tag> Bob </name-tag> https://www.html5rocks.com/ja/tutorials/ webcomponents/shadowdom/
ࢀߟʹͨ͠ࢿྉ ❖ https://www.html5rocks.com/ja/tutorials/ webcomponents/shadowdom/ ❖ https://www.html5rocks.com/ja/tutorials/ webcomponents/shadowdom-201/ ❖ https://developers.google.com/web/fundamentals/ web-components/shadowdom?hl=ja