|
| 1 | +/** |
| 2 | + * Copyright 2026 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import { css, html, LitElement, type TemplateResult } from 'lit'; |
| 14 | +import { customElement } from 'lit/decorators.js'; |
| 15 | + |
| 16 | +import { SlotAttributePropagationController } from '../index.js'; |
| 17 | + |
| 18 | +declare global { |
| 19 | + interface HTMLElementTagNameMap { |
| 20 | + 'demo-slot-propagation-default': DemoSlotPropagationDefault; |
| 21 | + 'demo-slot-propagation-named': DemoSlotPropagationNamed; |
| 22 | + 'demo-slot-propagation-selector': DemoSlotPropagationSelector; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// ───────────────────────────────────────── |
| 27 | +// DEFAULT (UNNAMED) SLOT DEMO HOST |
| 28 | +// ───────────────────────────────────────── |
| 29 | + |
| 30 | +/** |
| 31 | + * @internal |
| 32 | + * |
| 33 | + * Storybook-only host demonstrating {@link SlotAttributePropagationController} |
| 34 | + * targeting the default (unnamed) slot, mirroring `ButtonGroupBase`'s usage: |
| 35 | + * no `slotName` and no `selector`, so every assigned element receives the |
| 36 | + * `size` attribute. |
| 37 | + */ |
| 38 | +@customElement('demo-slot-propagation-default') |
| 39 | +export class DemoSlotPropagationDefault extends LitElement { |
| 40 | + static override styles = css` |
| 41 | + :host { |
| 42 | + display: flex; |
| 43 | + gap: 8px; |
| 44 | + flex-wrap: wrap; |
| 45 | + } |
| 46 | + `; |
| 47 | + |
| 48 | + static override properties = { |
| 49 | + size: { type: String }, |
| 50 | + }; |
| 51 | + |
| 52 | + declare size: string; |
| 53 | + |
| 54 | + constructor() { |
| 55 | + super(); |
| 56 | + this.size = 'm'; |
| 57 | + } |
| 58 | + |
| 59 | + private readonly _sizePropagation = new SlotAttributePropagationController( |
| 60 | + this, |
| 61 | + { |
| 62 | + attribute: 'size', |
| 63 | + getValue: () => this.size, |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + /** Re-propagates to elements assigned after the first render. */ |
| 68 | + private _handleSlotchange(): void { |
| 69 | + this._sizePropagation.propagate(); |
| 70 | + } |
| 71 | + |
| 72 | + protected override render(): TemplateResult { |
| 73 | + return html` |
| 74 | + <slot @slotchange=${this._handleSlotchange}></slot> |
| 75 | + `; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// ───────────────────────────── |
| 80 | +// NAMED SLOT DEMO HOST |
| 81 | +// ───────────────────────────── |
| 82 | + |
| 83 | +/** |
| 84 | + * @internal |
| 85 | + * |
| 86 | + * Storybook-only host demonstrating {@link SlotAttributePropagationController} |
| 87 | + * targeting a named slot, mirroring `IllustratedMessageBase`'s usage: |
| 88 | + * `slotName: 'actions'` so only elements assigned to the `actions` slot |
| 89 | + * receive the `size` attribute. A default slot is also rendered to prove |
| 90 | + * elements assigned there are left untouched. |
| 91 | + */ |
| 92 | +@customElement('demo-slot-propagation-named') |
| 93 | +export class DemoSlotPropagationNamed extends LitElement { |
| 94 | + static override styles = css` |
| 95 | + :host { |
| 96 | + display: flex; |
| 97 | + flex-direction: column; |
| 98 | + gap: 8px; |
| 99 | + } |
| 100 | + .actions { |
| 101 | + display: flex; |
| 102 | + gap: 8px; |
| 103 | + flex-wrap: wrap; |
| 104 | + } |
| 105 | + `; |
| 106 | + |
| 107 | + static override properties = { |
| 108 | + size: { type: String }, |
| 109 | + }; |
| 110 | + |
| 111 | + declare size: string; |
| 112 | + |
| 113 | + constructor() { |
| 114 | + super(); |
| 115 | + this.size = 'm'; |
| 116 | + } |
| 117 | + |
| 118 | + private readonly _sizePropagation = new SlotAttributePropagationController( |
| 119 | + this, |
| 120 | + { |
| 121 | + attribute: 'size', |
| 122 | + getValue: () => this.size, |
| 123 | + slotName: 'actions', |
| 124 | + } |
| 125 | + ); |
| 126 | + |
| 127 | + /** Re-propagates to elements assigned to the `actions` slot after the first render. */ |
| 128 | + private _handleActionsSlotchange(): void { |
| 129 | + this._sizePropagation.propagate(); |
| 130 | + } |
| 131 | + |
| 132 | + protected override render(): TemplateResult { |
| 133 | + return html` |
| 134 | + <slot></slot> |
| 135 | + <div class="actions"> |
| 136 | + <slot |
| 137 | + name="actions" |
| 138 | + @slotchange=${this._handleActionsSlotchange} |
| 139 | + ></slot> |
| 140 | + </div> |
| 141 | + `; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// ──────────────────────────────── |
| 146 | +// SELECTOR-FILTERED DEMO HOST |
| 147 | +// ──────────────────────────────── |
| 148 | + |
| 149 | +/** |
| 150 | + * @internal |
| 151 | + * |
| 152 | + * Storybook-only host demonstrating the {@link SlotAttributePropagationController} |
| 153 | + * `selector` option: only default-slot elements matching `.target` receive |
| 154 | + * the `variant` attribute; sibling elements without that class are left |
| 155 | + * untouched. |
| 156 | + */ |
| 157 | +@customElement('demo-slot-propagation-selector') |
| 158 | +export class DemoSlotPropagationSelector extends LitElement { |
| 159 | + static override styles = css` |
| 160 | + :host { |
| 161 | + display: flex; |
| 162 | + gap: 8px; |
| 163 | + flex-wrap: wrap; |
| 164 | + } |
| 165 | + `; |
| 166 | + |
| 167 | + static override properties = { |
| 168 | + variant: { type: String }, |
| 169 | + }; |
| 170 | + |
| 171 | + declare variant: string; |
| 172 | + |
| 173 | + constructor() { |
| 174 | + super(); |
| 175 | + this.variant = 'accent'; |
| 176 | + } |
| 177 | + |
| 178 | + private readonly _variantPropagation = new SlotAttributePropagationController( |
| 179 | + this, |
| 180 | + { |
| 181 | + attribute: 'variant', |
| 182 | + getValue: () => this.variant, |
| 183 | + selector: '.target', |
| 184 | + } |
| 185 | + ); |
| 186 | + |
| 187 | + /** Re-propagates to matching elements assigned after the first render. */ |
| 188 | + private _handleSlotchange(): void { |
| 189 | + this._variantPropagation.propagate(); |
| 190 | + } |
| 191 | + |
| 192 | + protected override render(): TemplateResult { |
| 193 | + return html` |
| 194 | + <slot @slotchange=${this._handleSlotchange}></slot> |
| 195 | + `; |
| 196 | + } |
| 197 | +} |
0 commit comments