Baseline Widely available
exportparts
ã°ãã¼ãã«å±æ§ ã§ã¯ããã¹ãããã·ã£ãã¦ããªã¼å
ã«åå¨ããè¦ç´ ããã¼ã (part
) åã§ã¨ã¯ã¹ãã¼ããã¦ããã®ã¹ã¿ã¤ã«ã鏿ã§ããããã«ãããã¨ãã§ãã¾ãã
ã·ã£ãã¦ããªã¼ã¯ãèå¥åãã¯ã©ã¹ãã¹ã¿ã¤ã«ããé常㮠DOM ã«å±ããã»ã¬ã¯ã¿ã¼ãã¯ã¨ãªã¼ã«ãã£ã¦å°éãããã¨ãã§ããªãå¤ç«ããæ§é ä½ã§ããã·ã£ãã¦ããªã¼è¦ç´ ã«é©ç¨ã§ãã 2 ã¤ã® HTML 屿§ããããããã使ç¨ããã¨ãå¤é¨ããã·ã£ãã¦ããªã¼ã« CSS ã¹ã¿ã¤ã«ãé©ç¨ãããã¨ãã§ãã¾ãããããã¯ã part
ããã³ exportparts
ã§ãã
ã°ãã¼ãã«å±æ§ part
ã¯ãã·ã£ãã¦ããªã¼è¦ç´ ããã®è¦ª DOM ããè¦ããããã«ãã¾ãããã¼ãåã¯ã ::part()
æ¬ä¼¼è¦ç´ ã®å¼æ°ã¨ãã¦ä½¿ç¨ããã¾ããããã«ãããã·ã£ãã¦ããªã¼å¤ã®è¦ç´ ã« CSS ã¹ã¿ã¤ã«ãé©ç¨ãããã¨ãã§ãã¾ãããã ãã ::part()
æ¬ä¼¼è¦ç´ ã¯è¦ª DOM ããã®ã¿è¡¨ç¤ºããã¾ããã¤ã¾ããã·ã£ãã¦ããªã¼ãå
¥ãåã«ãªã£ã¦ããå ´åããã¼ãã¯ç´æ¥ã®è¦ªä»¥å¤ã®ç¥å
ã«ã¯è¡¨ç¤ºããã¾ãããexportparts
屿§ã¯ããã®å¶éã解決ãã¾ãã
exportparts
屿§ã¯ãã·ã£ãã¦ããªã¼ã®ä¸é¨ãã·ã£ã㦠DOM ã®å¤é¨ããè¦ããããã«ãã¾ãããã®æ¦å¿µã¯ãã¨ã¯ã¹ãã¼ããã¨å¼ã°ãã¾ããexportparts
屿§ã¯ãã·ã£ãã¦ããªã¼ãé¢é£ä»ãããã¦ããè¦ç´ ã§ãããè¦ç´ ã®ã·ã£ãã¦ãã¹ãã«é
ç½®ããã¾ãããã®å±æ§ã®å¤ã¯ãã·ã£ãã¦ããªã¼å
ã«åå¨ãã part
åãã«ã³ãã§åºåã£ããªã¹ãã§ãããããã®ååã¯ãç¾å¨ã®æ§é ã®å¤é¨ã® DOM ã§å©ç¨å¯è½ã«ãªãã¾ãã
<template id="ancestor-component">
<nested-component exportparts="part1, part2, part5"></nested-component>
</template>
part
ãã¨ã¯ã¹ãã¼ãããå ´åã以ä¸ã®ã¹ããããã«ç¤ºãããã«ããã®ãã¼ãã«å¥ã®ååãå²ãå½ã¦ããªãã·ã§ã³ãããã¾ãã exportparts
屿§ã®å¤ã¯ãå®éã«ã¯ããã¼ãåã¨å²ãå½ã¦ãããååã¨ã®ã«ã³ãåºåãã®ãªã¹ãã§ãããããã£ã¦ãä¸è¨ã®ã³ã¼ãã¹ããããã® exportparts
屿§ã¯ã exportparts="part1:part1, part2:part2, part5:part5
ã¨åçã§ããããããã part
ãåãååã§ã¨ã¯ã¹ãã¼ãããããã¨ã示ãã¦ãã¾ããããããã®é
ç®ã§ã¯ãæåã®æååã¯ã·ã£ãã¦ããªã¼å
ã®ãã¼ãã®ååãæå®ãã2 ã¤ç®ã®æååã¯ãã¼ããå¤é¨ã«å
¬éãããååãæå®ãã¾ãã
<template id="ancestor-component">
<nested-component
exportparts="part1:exposed1, part2:exposed2"></nested-component>
</template>
ä¾ åºæ¬çãªã³ã³ãã¼ãã³ã
å
¥ãåã«ãªã£ãæ§æé¨åå
ã®é¨åã対象ã¨ããããã« exportparts
ãã©ã®ããã«ä½¿ç¨ããããã示ãããã«ãæ§æé¨åã使ãããããå¥ã®æ§æé¨åã«å
¥ãåã«ãã¾ãã
ã¾ããå¥ã®é¨åã§å²ãã«ã¼ãé¨åã使ãã¾ããããã¾ããæ°ãã使ããè¦ç´ ã使ç¨ããã¹ãããã«ãã¬ã¼ã³ããã¹ããã³ã³ãã³ãã¨ãã¦å ¥åãã¾ãã
<template id="card-component-template">
<style>
:host {
display: block;
}
</style>
<div class="base" part="base">
<div part="header"><slot name="header_slot"></slot></div>
<div part="body"><slot name="body_slot"></slot></div>
<div part="footer"><slot name="footer_slot"></slot></div>
</div>
</template>
<card-component>
<p slot="header_slot">ããã¯ãããã¼ã§ã</p>
<p slot="body_slot">ããã¯æ¬æã§ã</p>
<p slot="footer_slot">ããã¯ããã¿ã¼ã§ã</p>
</card-component>
JavaScript
JavaScript ã使ç¨ãã¦ãä¸è¨ã® HTML ã§å®ç¾©ããã¦ã§ãã³ã³ãã¼ãã³ããå®ç¾©ãã¾ãã
customElements.define(
"card-component",
class extends HTMLElement {
constructor() {
super(); // ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ã¯å¸¸ã«æåã« super ãå¼ã³åºãã¦ãã ããã
const cardComponent = document.getElementById(
"card-component-template",
).content;
const shadowRoot = this.attachShadow({
mode: "open",
});
shadowRoot.appendChild(cardComponent.cloneNode(true));
}
},
);
CSS
<card-component>
ã·ã£ãã¦ããªã¼ã®åé¨åã®ã¹ã¿ã¤ã«ãã ::part
æ¬ä¼¼è¦ç´ ã使ç¨ãã¦è¨å®ãã¾ãã
::part(body) {
color: red;
font-style: italic;
}
çµæ å
¥ãåã®ã³ã³ãã¼ãã³ã
ä¸è¨ã® <card-component>
ã®ä¾ã«ç¶ãã¦ã<card-component>
ãå¥ã®ã³ã³ãã¼ãã³ãï¼ãã®ä¾ã§ã¯ <card-wrapper>
ã³ã³ãã¼ãã³ãï¼ã§å²ãã§ãå
¥ãåã³ã³ãã¼ãã³ãã使ãã¾ããæ¬¡ã«ãã³ã³ãã¼ãã³ãã®ã·ã£ãã¦ããªã¼ã®å¤ããã¹ã¿ã¤ã«ãè¨å®ã§ããããã«ãå
¥ãåã³ã³ãã¼ãã³ããã exportparts
屿§ã§ã¨ã¯ã¹ãã¼ãããé¨åã使ãã¾ãã
<template id="card-component-template">
<style>
:host {
display: block;
}
</style>
<div class="base" part="base">
<div part="header"><slot name="header_slot"></slot></div>
<div part="body"><slot name="body_slot"></slot></div>
<div part="footer"><slot name="footer_slot"></slot></div>
</div>
</template>
<template id="card-wrapper">
<style>
:host {
display: block;
}
</style>
<card-component exportparts="base, header, body">
<slot name="H" slot="header_slot"></slot>
<slot name="B" slot="body_slot"></slot>
<slot name="F" slot="footer_slot"></slot>
</card-component>
</template>
æ¯è¼ã®ããã«ãã«ã¹ã¿ã è¦ç´ <card-wrapper>
ããã³ <card-component>
ãå«ãã¾ãã
<h2>ã«ã¼ãã©ããã¼</h2>
<card-wrapper>
<p slot="H">ããã¯ãããã¼ã§ã</p>
<p slot="B">ããã¯æ¬æã§ã</p>
<p slot="F">ããã¯ããã¿ã¼ã§ã</p>
</card-wrapper>
<h2>ã«ã¼ãã³ã³ãã¼ãã³ãããã</h2>
<card-component>
<p slot="header_slot">ããã¯ãããã¼ã§ã</p>
<p slot="body_slot">ããã¯æ¬æã§ã</p>
<p slot="footer_slot">ããã¯ããã¿ã¼ã§ã</p>
</card-component>
JavaScript
customElements.define(
"card-component",
class extends HTMLElement {
constructor() {
super(); // ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ã¯ãå¸¸ã«æåã« super ãå¼ã³åºã
const cardComponent = document.getElementById(
"card-component-template",
).content;
const shadowRoot = this.attachShadow({
mode: "open",
});
shadowRoot.appendChild(cardComponent.cloneNode(true));
}
},
);
customElements.define(
"card-wrapper",
class extends HTMLElement {
constructor() {
super(); // ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ã¯ãå¸¸ã«æåã« super ãå¼ã³åºã
const cardWrapper = document.getElementById("card-wrapper").content;
const shadowRoot = this.attachShadow({
mode: "open",
});
shadowRoot.appendChild(cardWrapper.cloneNode(true));
}
},
);
CSS
ããã§ã<card-wrapper>
å
ã«æ¬¡ã®ããã«å
¥ãåã«ããå ´åã<card-component>
ã®é¨åãç´æ¥å¯¾è±¡ã¨ãããã¨ãã§ãã¾ãã
h2 {
background-color: #dedede;
}
card-wrapper,
card-component {
border: 1px dashed blue;
width: fit-content;
}
::part(body) {
color: red;
font-style: italic;
}
::part(header),
::part(footer) {
font-weight: bold;
}
çµæ
footer
ã¯ãexportparts
ã«å«ã¾ãã¦ããªããããå
¥ãåã«ããå ´åã太åã«ã¯ãªãã¾ããã
ã¨ã¯ã¹ãã¼ããããã¼ãã®ååã夿´ããã«ã¯ãå²ãå½ã¦ããããã¼ããã«ã³ãã§åºåã£ããªã¹ããè¨è¼ãã¾ããåå²ãå½ã¦ããããã¼ãã«ã¯ãå
ã®åã¨ã¨ã¯ã¹ãã¼ãããååãã³ãã³ (:
) ã§åºåããã¦å«ã¾ãã¾ãã
以åã® <card-wrapper>
ã«ã¹ã¿ã è¦ç´ ããåãããã³ã°æ§æã使ç¨ãã¦æ´æ°ãã¾ã ï¼ã¨ã¯ã¹ãã¼ãããããã¼ãã®ãªã¹ããã body
ãçç¥ãã¾ãï¼ã
<template id="card-component-template">
<div class="base" part="base">
<div part="header"><slot name="header_slot"></slot></div>
<div part="body"><slot name="body_slot"></slot></div>
<div part="footer"><slot name="footer_slot"></slot></div>
</div>
</template>
<card-wrapper>
<p slot="H">ããã¯ãããã¼ã§ã</p>
<p slot="B">ããã¯æ¬æã§ã</p>
<p slot="F">ããã¯ããã¿ã¼ã§ã</p>
</card-wrapper>
<template id="card-wrapper">
<card-component
exportparts="
base:card__base,
header:card__header,
footer:card__footer
">
<span slot="header_slot"><slot name="H"></slot></span>
<span slot="body_slot"><slot name="B"></slot></span>
<span slot="footer_slot"><slot name="F"></slot></span>
</card-component>
</template>
JavaScript
customElements.define(
"card-component",
class extends HTMLElement {
constructor() {
super(); // ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ã¯ãå¸¸ã«æåã« super ãå¼ã³åºã
const cardComponent = document.getElementById(
"card-component-template",
).content;
const shadowRoot = this.attachShadow({
mode: "open",
});
shadowRoot.appendChild(cardComponent.cloneNode(true));
}
},
);
customElements.define(
"card-wrapper",
class extends HTMLElement {
constructor() {
super(); // ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ã¯ãå¸¸ã«æåã« super ãå¼ã³åºã
const cardWrapper = document.getElementById("card-wrapper").content;
const shadowRoot = this.attachShadow({
mode: "open",
});
shadowRoot.appendChild(cardWrapper.cloneNode(true));
}
},
);
CSS
<card-wrapper>
å
ãã <card-component>
ã®é¨åãã¿ã¼ã²ããã¨ãã¦ãã¨ã¯ã¹ãã¼ããããé¨åã®ã¿ãå
¬éããã¦ããé¨ååã使ç¨ãã¦ã¹ã¿ã¤ã«ãè¨å®ãããã¨ãã§ãã¾ãã
/* ã¨ã¯ã¹ãã¼ããããã¼ãã®ååã鏿 */
::part(card__header) {
font-weight: bold;
}
/* ä½ã鏿ããªãããããã®ãã¼ãåã¯ã¨ã¯ã¹ãã¼ãããã¦ããªã */
::part(footer),
::part(body) {
font-weight: bold;
}
çµæ 仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
part
屿§<template>
è¦ç´ 㨠<slot>
è¦ç´ ::part
ããã³ ::slotted
æ¬ä¼¼è¦ç´ ShadowRoot
ã¤ã³ã¿ã¼ãã§ã¤ã¹Element.part
ããããã£RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4