@m3e/segmented-button 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +174 -0
- package/cem.config.mjs +16 -0
- package/demo/index.html +105 -0
- package/dist/css-custom-data.json +162 -0
- package/dist/custom-elements.json +548 -0
- package/dist/html-custom-data.json +55 -0
- package/dist/index.js +642 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +269 -0
- package/dist/index.min.js.map +1 -0
- package/dist/src/ButtonSegmentElement.d.ts +105 -0
- package/dist/src/ButtonSegmentElement.d.ts.map +1 -0
- package/dist/src/SegmentedButtonElement.d.ts +89 -0
- package/dist/src/SegmentedButtonElement.d.ts.map +1 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.d.ts.map +1 -0
- package/eslint.config.mjs +13 -0
- package/package.json +48 -0
- package/rollup.config.js +32 -0
- package/src/ButtonSegmentElement.ts +407 -0
- package/src/SegmentedButtonElement.ts +208 -0
- package/src/index.ts +2 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { css, CSSResultGroup, html, LitElement, PropertyValues, unsafeCSS } from "lit";
|
|
2
|
+
import { customElement, property, query } from "lit/decorators.js";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
AttachInternals,
|
|
6
|
+
Checked,
|
|
7
|
+
DesignToken,
|
|
8
|
+
Dirty,
|
|
9
|
+
Disabled,
|
|
10
|
+
hasAssignedNodes,
|
|
11
|
+
M3eFocusRingElement,
|
|
12
|
+
M3eRippleElement,
|
|
13
|
+
M3eStateLayerElement,
|
|
14
|
+
Role,
|
|
15
|
+
Touched,
|
|
16
|
+
} from "@m3e/core";
|
|
17
|
+
|
|
18
|
+
import { selectionManager } from "@m3e/core/a11y";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @summary
|
|
22
|
+
* A option that can be selected within a segmented button.
|
|
23
|
+
*
|
|
24
|
+
* @description
|
|
25
|
+
* The `m3e-button-segment` component represents a single selectable option within a segmented button group.
|
|
26
|
+
* It behaves like a toggle-able button, supporting icon and label content, selection state, and accessibility
|
|
27
|
+
* roles. Segments are visually unified but independently interactive, adapting shape, color, and ripple feedback
|
|
28
|
+
* based on their position and state.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* The following example illustrates a single-select segmented button with four segments.
|
|
32
|
+
* ```html
|
|
33
|
+
* <m3e-segmented-button>
|
|
34
|
+
* <m3e-button-segment checked>8 oz</m3e-button-segment>
|
|
35
|
+
* <m3e-button-segment>12 oz</m3e-button-segment>
|
|
36
|
+
* <m3e-button-segment>16 oz</m3e-button-segment>
|
|
37
|
+
* <m3e-button-segment>20 oz</m3e-button-segment>
|
|
38
|
+
* </m3e-segmented-button>
|
|
39
|
+
* ```
|
|
40
|
+
* @example
|
|
41
|
+
* The next example illustrates a multiselect segmented button designated using the `multi` attribute.
|
|
42
|
+
* ```html
|
|
43
|
+
* <m3e-segmented-button multi>
|
|
44
|
+
* <m3e-button-segment checked>$</m3e-button-segment>
|
|
45
|
+
* <m3e-button-segment checked>$$</m3e-button-segment>
|
|
46
|
+
* <m3e-button-segment>$$$</m3e-button-segment>
|
|
47
|
+
* <m3e-button-segment>$$$$</m3e-button-segment>
|
|
48
|
+
* </m3e-segmented-button>
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @tag m3e-button-segment
|
|
52
|
+
*
|
|
53
|
+
* @slot - Renders the label of the option.
|
|
54
|
+
* @slot icon - Renders an icon before the option's label.
|
|
55
|
+
*
|
|
56
|
+
* @attr checked - Whether the element is checked.
|
|
57
|
+
* @attr disabled - Whether the element is disabled.
|
|
58
|
+
* @attr value - A string representing the value of the segment.
|
|
59
|
+
*
|
|
60
|
+
* @fires input - Emitted when the checked state changes.
|
|
61
|
+
* @fires change - Emitted when the checked state changes.
|
|
62
|
+
*
|
|
63
|
+
* @cssprop --m3e-segmented-button-height - Total height of the segmented button.
|
|
64
|
+
* @cssprop --m3e-segmented-button-outline-thickness - Thickness of the button’s border.
|
|
65
|
+
* @cssprop --m3e-segmented-button-outline-color - Color of the button’s border.
|
|
66
|
+
* @cssprop --m3e-segmented-button-padding-start - Padding on the leading edge of the button content.
|
|
67
|
+
* @cssprop --m3e-segmented-button-padding-end - Padding on the trailing edge of the button content.
|
|
68
|
+
* @cssprop --m3e-segmented-button-spacing - Horizontal gap between icon and label.
|
|
69
|
+
* @cssprop --m3e-segmented-button-font-size - Font size of the label text.
|
|
70
|
+
* @cssprop --m3e-segmented-button-font-weight - Font weight of the label text.
|
|
71
|
+
* @cssprop --m3e-segmented-button-line-height - Line height of the label text.
|
|
72
|
+
* @cssprop --m3e-segmented-button-tracking - Letter spacing of the label text.
|
|
73
|
+
* @cssprop --m3e-segmented-button-with-icon-padding-start - Leading padding when an icon is present.
|
|
74
|
+
* @cssprop --m3e-segmented-button-selected-container-color - Background color of a selected segment.
|
|
75
|
+
* @cssprop --m3e-segmented-button-selected-container-hover-color - Hover state-layer color for selected segments.
|
|
76
|
+
* @cssprop --m3e-segmented-button-selected-container-focus-color - Focus state-layer color for selected segments.
|
|
77
|
+
* @cssprop --m3e-segmented-button-selected-ripple-color - Ripple color for selected segments.
|
|
78
|
+
* @cssprop --m3e-segmented-button-selected-label-text-color - Label text color for selected segments.
|
|
79
|
+
* @cssprop --m3e-segmented-button-selected-icon-color - Icon color for selected segments.
|
|
80
|
+
* @cssprop --m3e-segmented-button-unselected-container-hover-color - Hover state-layer color for unselected segments.
|
|
81
|
+
* @cssprop --m3e-segmented-button-unselected-container-focus-color - Focus state-layer color for unselected segments.
|
|
82
|
+
* @cssprop --m3e-segmented-button-unselected-ripple-color - Ripple color for unselected segments.
|
|
83
|
+
* @cssprop --m3e-segmented-button-unselected-label-text-color - Label text color for unselected segments.
|
|
84
|
+
* @cssprop --m3e-segmented-button-unselected-icon-color - Icon color for unselected segments.
|
|
85
|
+
* @cssprop --m3e-segmented-button-icon-size - Font size of the icon.
|
|
86
|
+
* @cssprop --m3e-segmented-button-disabled-outline-color - Base color for disabled segment borders.
|
|
87
|
+
* @cssprop --m3e-segmented-button-disabled-outline-opacity - Opacity applied to disabled segment borders.
|
|
88
|
+
* @cssprop --m3e-segmented-button-disabled-label-text-color - Base color for disabled label text.
|
|
89
|
+
* @cssprop --m3e-segmented-button-disabled-label-text-opacity - Opacity applied to disabled label text.
|
|
90
|
+
* @cssprop --m3e-segmented-button-disabled-icon-color - Base color for disabled icons.
|
|
91
|
+
* @cssprop --m3e-segmented-button-disabled-icon-opacity - Opacity applied to disabled icons.
|
|
92
|
+
*/
|
|
93
|
+
@customElement("m3e-button-segment")
|
|
94
|
+
export class M3eButtonSegmentElement extends Dirty(
|
|
95
|
+
Touched(Checked(Disabled(AttachInternals(Role(LitElement, "radio"), true))))
|
|
96
|
+
) {
|
|
97
|
+
/** The styles of the element. */
|
|
98
|
+
static override styles: CSSResultGroup = css`
|
|
99
|
+
:host {
|
|
100
|
+
display: inline-block;
|
|
101
|
+
position: relative;
|
|
102
|
+
vertical-align: middle;
|
|
103
|
+
outline: none;
|
|
104
|
+
user-select: none;
|
|
105
|
+
flex: 1 1 auto;
|
|
106
|
+
min-width: 0;
|
|
107
|
+
}
|
|
108
|
+
.base {
|
|
109
|
+
display: inline-flex;
|
|
110
|
+
vertical-align: middle;
|
|
111
|
+
align-items: center;
|
|
112
|
+
width: 100%;
|
|
113
|
+
height: calc(var(--m3e-segmented-button-height, 2.5rem) + ${DesignToken.density.calc(-3)});
|
|
114
|
+
box-sizing: border-box;
|
|
115
|
+
border-width: var(--m3e-segmented-button-outline-thickness, 1px);
|
|
116
|
+
border-color: var(--m3e-segmented-button-outline-color, ${DesignToken.color.outline});
|
|
117
|
+
border-style: solid;
|
|
118
|
+
border-radius: inherit;
|
|
119
|
+
border-left-style: var(--_segmented-button-left-border, solid);
|
|
120
|
+
transition: ${unsafeCSS(
|
|
121
|
+
`background-color ${DesignToken.motion.duration.short4} ${DesignToken.motion.easing.standard}`
|
|
122
|
+
)};
|
|
123
|
+
}
|
|
124
|
+
.touch {
|
|
125
|
+
position: absolute;
|
|
126
|
+
height: 3rem;
|
|
127
|
+
left: 0;
|
|
128
|
+
right: 0;
|
|
129
|
+
}
|
|
130
|
+
.wrapper {
|
|
131
|
+
width: 100%;
|
|
132
|
+
overflow: hidden;
|
|
133
|
+
display: inline-flex;
|
|
134
|
+
align-items: center;
|
|
135
|
+
padding-inline-start: var(--m3e-segmented-button-padding-start, 1rem);
|
|
136
|
+
padding-inline-end: var(--m3e-segmented-button-padding-end, 1rem);
|
|
137
|
+
column-gap: var(--m3e-segmented-button-spacing, 0.5rem);
|
|
138
|
+
}
|
|
139
|
+
.label {
|
|
140
|
+
font-size: var(--m3e-segmented-button-font-size, ${DesignToken.typescale.standard.label.large.fontSize});
|
|
141
|
+
font-weight: var(--m3e-segmented-button-font-weight, ${DesignToken.typescale.standard.label.large.fontWeight});
|
|
142
|
+
line-height: var(--m3e-segmented-button-line-height, ${DesignToken.typescale.standard.label.large.lineHeight});
|
|
143
|
+
letter-spacing: var(--m3e-segmented-button-tracking, ${DesignToken.typescale.standard.label.large.tracking});
|
|
144
|
+
justify-self: center;
|
|
145
|
+
flex: 1 1 auto;
|
|
146
|
+
white-space: nowrap;
|
|
147
|
+
overflow: hidden;
|
|
148
|
+
text-overflow: ellipsis;
|
|
149
|
+
transition: ${unsafeCSS(`color ${DesignToken.motion.duration.short4} ${DesignToken.motion.easing.standard}`)};
|
|
150
|
+
}
|
|
151
|
+
.icon {
|
|
152
|
+
display: flex;
|
|
153
|
+
align-items: center;
|
|
154
|
+
justify-content: center;
|
|
155
|
+
transition: ${unsafeCSS(`color ${DesignToken.motion.duration.short4} ${DesignToken.motion.easing.standard}`)};
|
|
156
|
+
}
|
|
157
|
+
:host(:not(:disabled)) {
|
|
158
|
+
cursor: pointer;
|
|
159
|
+
}
|
|
160
|
+
:host([checked]:not(.-hide-selection)) .wrapper,
|
|
161
|
+
:host(.-with-icon) .wrapper {
|
|
162
|
+
padding-inline-start: var(--m3e-segmented-button-with-icon-padding-start, 0.75rem);
|
|
163
|
+
}
|
|
164
|
+
:host(:not(:disabled)[checked]) .base {
|
|
165
|
+
background-color: var(--m3e-segmented-button-selected-container-color, ${DesignToken.color.secondaryContainer});
|
|
166
|
+
--m3e-state-layer-hover-color: var(
|
|
167
|
+
--m3e-segmented-button-selected-container-hover-color,
|
|
168
|
+
${DesignToken.color.onSecondaryContainer}
|
|
169
|
+
);
|
|
170
|
+
--m3e-state-layer-focus-color: var(
|
|
171
|
+
--m3e-segmented-button-selected-container-focus-color,
|
|
172
|
+
${DesignToken.color.onSecondaryContainer}
|
|
173
|
+
);
|
|
174
|
+
--m3e-ripple-color: var(--m3e-segmented-button-selected-ripple-color, ${DesignToken.color.onSecondaryContainer});
|
|
175
|
+
}
|
|
176
|
+
:host(:not(:disabled)[checked]) .label {
|
|
177
|
+
color: var(--m3e-segmented-button-selected-label-text-color, ${DesignToken.color.onSecondaryContainer});
|
|
178
|
+
}
|
|
179
|
+
:host(:not(:disabled)[checked]) .icon {
|
|
180
|
+
color: var(--m3e-segmented-button-selected-icon-color, ${DesignToken.color.onSecondaryContainer});
|
|
181
|
+
}
|
|
182
|
+
:host(:not(:disabled):not([checked])) .base {
|
|
183
|
+
--m3e-state-layer-hover-color: var(
|
|
184
|
+
--m3e-segmented-button-unselected-container-hover-color,
|
|
185
|
+
${DesignToken.color.onSurface}
|
|
186
|
+
);
|
|
187
|
+
--m3e-state-layer-focus-color: var(
|
|
188
|
+
--m3e-segmented-button-unselected-container-focus-color,
|
|
189
|
+
${DesignToken.color.onSurface}
|
|
190
|
+
);
|
|
191
|
+
--m3e-ripple-color: var(--m3e-segmented-button-unselected-ripple-color, ${DesignToken.color.onSurface});
|
|
192
|
+
}
|
|
193
|
+
:host(:not(:disabled):not([checked])) .label {
|
|
194
|
+
color: var(--m3e-segmented-button-unselected-label-text-color, ${DesignToken.color.onSurface});
|
|
195
|
+
}
|
|
196
|
+
:host(:not(:disabled):not([checked])) .icon {
|
|
197
|
+
color: var(--m3e-segmented-button-unselected-icon-color, ${DesignToken.color.onSurface});
|
|
198
|
+
}
|
|
199
|
+
:host(:not(.-with-icon)) .icon {
|
|
200
|
+
margin-inline-start: calc(0px - var(--m3e-segmented-button-spacing, 0.5rem));
|
|
201
|
+
transition: margin-inline-start ${DesignToken.motion.spring.fastEffects};
|
|
202
|
+
}
|
|
203
|
+
.check,
|
|
204
|
+
::slotted([slot="icon"]) {
|
|
205
|
+
width: 1em;
|
|
206
|
+
font-size: var(--m3e-segmented-button-icon-size, 1.125rem) !important;
|
|
207
|
+
}
|
|
208
|
+
:host(:not([checked])) .check,
|
|
209
|
+
:host(.-hide-selection) .check,
|
|
210
|
+
:host(.-hide-selection:not(.-with-icon)) .icon {
|
|
211
|
+
display: none;
|
|
212
|
+
}
|
|
213
|
+
:host([checked]) .icon {
|
|
214
|
+
margin-inline-start: 0;
|
|
215
|
+
}
|
|
216
|
+
:host([checked]:not(.-hide-selection)) ::slotted([slot="icon"]) {
|
|
217
|
+
display: none !important;
|
|
218
|
+
}
|
|
219
|
+
:host(:disabled) .base {
|
|
220
|
+
border-color: color-mix(
|
|
221
|
+
in srgb,
|
|
222
|
+
var(--m3e-segmented-button-disabled-outline-color, ${DesignToken.color.onSurface})
|
|
223
|
+
var(--m3e-segmented-button-disabled-outline-opacity, 12%),
|
|
224
|
+
transparent
|
|
225
|
+
);
|
|
226
|
+
border-right-color: var(
|
|
227
|
+
--_segmented-button-disabled-outline-color,
|
|
228
|
+
color-mix(
|
|
229
|
+
in srgb,
|
|
230
|
+
var(--m3e-segmented-button-disabled-outline-color, ${DesignToken.color.onSurface})
|
|
231
|
+
var(--m3e-segmented-button-disabled-outline-opacity, 12%),
|
|
232
|
+
transparent
|
|
233
|
+
)
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
:host(:disabled) .label {
|
|
237
|
+
color: color-mix(
|
|
238
|
+
in srgb,
|
|
239
|
+
var(--m3e-segmented-button-disabled-label-text-color, ${DesignToken.color.onSurface})
|
|
240
|
+
var(--m3e-segmented-button-disabled-label-text-opacity, 38%),
|
|
241
|
+
transparent
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
:host(:disabled) .icon {
|
|
245
|
+
color: color-mix(
|
|
246
|
+
in srgb,
|
|
247
|
+
var(--m3e-segmented-button-disabled-icon-color, ${DesignToken.color.onSurface})
|
|
248
|
+
var(--m3e-segmented-button-disabled-icon-opacity, 38%),
|
|
249
|
+
transparent
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
@media (prefers-reduced-motion) {
|
|
253
|
+
.base,
|
|
254
|
+
.icon,
|
|
255
|
+
.label {
|
|
256
|
+
transition: none;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
@media (forced-colors: active) {
|
|
260
|
+
.base,
|
|
261
|
+
.icon,
|
|
262
|
+
.label {
|
|
263
|
+
transition: none !important;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
:host(:disabled) .label,
|
|
267
|
+
:host(:disabled) .icon {
|
|
268
|
+
color: GrayText;
|
|
269
|
+
}
|
|
270
|
+
:host(:not([selected]):not(:disabled)) .label,
|
|
271
|
+
:host(:not([selected]):not(:disabled)) .icon {
|
|
272
|
+
color: ButtonText;
|
|
273
|
+
}
|
|
274
|
+
:host(:not(:disabled)[checked]) .base {
|
|
275
|
+
background-color: ButtonText;
|
|
276
|
+
}
|
|
277
|
+
:host(:not(:disabled)[checked]) .label,
|
|
278
|
+
:host(:not(:disabled)[checked]) .icon {
|
|
279
|
+
forced-color-adjust: none;
|
|
280
|
+
color: ButtonFace;
|
|
281
|
+
}
|
|
282
|
+
:host {
|
|
283
|
+
--m3e-segmented-button-outline-color: ButtonText;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
`;
|
|
287
|
+
|
|
288
|
+
/** @private */ @query(".focus-ring") private readonly _focusRing?: M3eFocusRingElement;
|
|
289
|
+
/** @private */ @query(".state-layer") private readonly _stateLayer?: M3eStateLayerElement;
|
|
290
|
+
/** @private */ @query(".ripple") private readonly _ripple?: M3eRippleElement;
|
|
291
|
+
/** @private */ readonly #clickHandler = (e: Event) => this.#handleClick(e);
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* A string representing the value of the segment.
|
|
295
|
+
* @default "on"
|
|
296
|
+
*/
|
|
297
|
+
@property() value = "on";
|
|
298
|
+
|
|
299
|
+
/** @inheritdoc */
|
|
300
|
+
override connectedCallback(): void {
|
|
301
|
+
super.connectedCallback();
|
|
302
|
+
this.addEventListener("click", this.#clickHandler);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** @inheritdoc */
|
|
306
|
+
override disconnectedCallback(): void {
|
|
307
|
+
super.disconnectedCallback();
|
|
308
|
+
this.removeEventListener("click", this.#clickHandler);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** @inheritdoc */
|
|
312
|
+
protected override firstUpdated(_changedProperties: PropertyValues<this>): void {
|
|
313
|
+
super.firstUpdated(_changedProperties);
|
|
314
|
+
[this._focusRing, this._stateLayer, this._ripple].forEach((x) => x?.attach(this));
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** @inheritdoc */
|
|
318
|
+
protected override update(changedProperties: PropertyValues<this>): void {
|
|
319
|
+
const segmentedButton = this.closest("m3e-segmented-button");
|
|
320
|
+
if (segmentedButton) {
|
|
321
|
+
this.role = segmentedButton.multi ? "button" : "radio";
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
super.update(changedProperties);
|
|
325
|
+
|
|
326
|
+
if (changedProperties.has("checked")) {
|
|
327
|
+
segmentedButton?.[selectionManager].notifySelectionChange(this);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (changedProperties.has("disabled")) {
|
|
331
|
+
if (this.disabled) {
|
|
332
|
+
if (this.nextElementSibling instanceof M3eButtonSegmentElement) {
|
|
333
|
+
if (!this.nextElementSibling.disabled) {
|
|
334
|
+
this.style.setProperty(
|
|
335
|
+
"--_segmented-button-disabled-outline-color",
|
|
336
|
+
`var(--m3e-segmented-button-outline-color, ${DesignToken.color.outline})`
|
|
337
|
+
);
|
|
338
|
+
} else {
|
|
339
|
+
this.style.removeProperty("--_segmented-button-disabled-outline-color");
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} else {
|
|
343
|
+
this.style.removeProperty("--_segmented-button-disabled-outline-color");
|
|
344
|
+
if (this.previousElementSibling instanceof M3eButtonSegmentElement) {
|
|
345
|
+
if (this.previousElementSibling.disabled) {
|
|
346
|
+
this.previousElementSibling.style.setProperty(
|
|
347
|
+
"--_segmented-button-disabled-outline-color",
|
|
348
|
+
`var(--m3e-segmented-button-outline-color, ${DesignToken.color.outline})`
|
|
349
|
+
);
|
|
350
|
+
} else {
|
|
351
|
+
this.previousElementSibling.style.removeProperty("--_segmented-button-disabled-outline-color");
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** @inheritdoc */
|
|
359
|
+
override render(): unknown {
|
|
360
|
+
return html`<div class="base">
|
|
361
|
+
<m3e-state-layer class="state-layer" ?disabled="${this.disabled}"></m3e-state-layer>
|
|
362
|
+
<m3e-focus-ring class="focus-ring" ?disabled="${this.disabled}"></m3e-focus-ring>
|
|
363
|
+
<m3e-ripple class="ripple" ?disabled="${this.disabled}"></m3e-ripple>
|
|
364
|
+
<div class="touch" aria-hidden="true"></div>
|
|
365
|
+
<div class="wrapper">
|
|
366
|
+
<div class="icon" aria-hidden="true">
|
|
367
|
+
<svg class="check" viewBox="0 -960 960 960">
|
|
368
|
+
<path fill="currentColor" d="M382-240 154-468l57-57 171 171 367-367 57 57-424 424Z" />
|
|
369
|
+
</svg>
|
|
370
|
+
<slot name="icon" @slotchange="${this.#handleIconSlotChange}"></slot>
|
|
371
|
+
</div>
|
|
372
|
+
<div class="label">
|
|
373
|
+
<slot></slot>
|
|
374
|
+
</div>
|
|
375
|
+
</div>
|
|
376
|
+
</div>`;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** @private */
|
|
380
|
+
#handleIconSlotChange(e: Event): void {
|
|
381
|
+
this.classList.toggle("-with-icon", hasAssignedNodes(<HTMLSlotElement>e.target));
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** @private */
|
|
385
|
+
#handleClick(e: Event): void {
|
|
386
|
+
if (e.defaultPrevented) return;
|
|
387
|
+
|
|
388
|
+
const segmentedButton = this.closest("m3e-segmented-button");
|
|
389
|
+
if (segmentedButton && (segmentedButton.multi || !this.checked)) {
|
|
390
|
+
const checked = this.checked;
|
|
391
|
+
this.checked = !this.checked;
|
|
392
|
+
|
|
393
|
+
if (this.dispatchEvent(new Event("input", { bubbles: true, composed: true, cancelable: true }))) {
|
|
394
|
+
this.closest("m3e-segmented-button")?.[selectionManager].notifySelectionChange(this);
|
|
395
|
+
this.dispatchEvent(new Event("change", { bubbles: true }));
|
|
396
|
+
} else {
|
|
397
|
+
this.checked = checked;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
declare global {
|
|
404
|
+
interface HTMLElementTagNameMap {
|
|
405
|
+
"m3e-button-segment": M3eButtonSegmentElement;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit";
|
|
2
|
+
import { customElement, property } from "lit/decorators.js";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
AttachInternals,
|
|
6
|
+
DesignToken,
|
|
7
|
+
Dirty,
|
|
8
|
+
Disabled,
|
|
9
|
+
FormAssociated,
|
|
10
|
+
formValue,
|
|
11
|
+
Labelled,
|
|
12
|
+
Role,
|
|
13
|
+
Touched,
|
|
14
|
+
} from "@m3e/core";
|
|
15
|
+
|
|
16
|
+
import { SelectionManager, selectionManager } from "@m3e/core/a11y";
|
|
17
|
+
|
|
18
|
+
import { M3eButtonSegmentElement } from "./ButtonSegmentElement";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @summary
|
|
22
|
+
* A button that allows a user to select from a limited set of options.
|
|
23
|
+
*
|
|
24
|
+
* @description
|
|
25
|
+
* The `m3e-segmented-button` component allows users to select one or more options from a horizontal group.
|
|
26
|
+
* Each segment behaves like a toggle-able button, supporting icon and label content, selection state, and
|
|
27
|
+
* accessibility roles. Built with Material Design 3 principles, it adapts shape, color, and ripple feedback
|
|
28
|
+
* based on interaction state and input modality. Segments are visually unified but independently interactive.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* The following example illustrates a single-select segmented button with four segments.
|
|
32
|
+
* ```html
|
|
33
|
+
* <m3e-segmented-button>
|
|
34
|
+
* <m3e-button-segment checked>8 oz</m3e-button-segment>
|
|
35
|
+
* <m3e-button-segment>12 oz</m3e-button-segment>
|
|
36
|
+
* <m3e-button-segment>16 oz</m3e-button-segment>
|
|
37
|
+
* <m3e-button-segment>20 oz</m3e-button-segment>
|
|
38
|
+
* </m3e-segmented-button>
|
|
39
|
+
* ```
|
|
40
|
+
* @example
|
|
41
|
+
* The next example illustrates a multiselect segmented button designated using the `multi` attribute.
|
|
42
|
+
* ```html
|
|
43
|
+
* <m3e-segmented-button multi>
|
|
44
|
+
* <m3e-button-segment checked>$</m3e-button-segment>
|
|
45
|
+
* <m3e-button-segment checked>$$</m3e-button-segment>
|
|
46
|
+
* <m3e-button-segment>$$$</m3e-button-segment>
|
|
47
|
+
* <m3e-button-segment>$$$$</m3e-button-segment>
|
|
48
|
+
* </m3e-segmented-button>
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @tag m3e-segmented-button
|
|
52
|
+
*
|
|
53
|
+
* @slot - Renders the segments of the button.
|
|
54
|
+
*
|
|
55
|
+
* @attr disabled - Whether the element is disabled.
|
|
56
|
+
* @attr hide-selection-indicator - Whether to hide the selection indicator.
|
|
57
|
+
* @attr multi - Whether multiple options can be selected.
|
|
58
|
+
* @attr name - The name that identifies the element when submitting the associated form.
|
|
59
|
+
*
|
|
60
|
+
* @fires input - Emitted when the checked state of a segment changes.
|
|
61
|
+
* @fires change - Emitted when the checked state of a segment changes.
|
|
62
|
+
*
|
|
63
|
+
* @cssprop --m3e-segmented-button-start-shape - Border radius for the first segment in a segmented button.
|
|
64
|
+
* @cssprop --m3e-segmented-button-end-shape - Border radius for the last segment in a segmented button.
|
|
65
|
+
*/
|
|
66
|
+
@customElement("m3e-segmented-button")
|
|
67
|
+
export class M3eSegmentedButtonElement extends Labelled(
|
|
68
|
+
Dirty(Touched(FormAssociated(Disabled(AttachInternals(Role(LitElement, "radiogroup"))))))
|
|
69
|
+
) {
|
|
70
|
+
/** The styles of the element. */
|
|
71
|
+
static override styles: CSSResultGroup = css`
|
|
72
|
+
:host {
|
|
73
|
+
display: inline-flex;
|
|
74
|
+
vertical-align: middle;
|
|
75
|
+
align-items: center;
|
|
76
|
+
}
|
|
77
|
+
::slotted(:first-child) {
|
|
78
|
+
border-radius: var(
|
|
79
|
+
--m3e-segmented-button-start-shape,
|
|
80
|
+
${DesignToken.shape.corner.full} ${DesignToken.shape.corner.none} ${DesignToken.shape.corner.none}
|
|
81
|
+
${DesignToken.shape.corner.full}
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
::slotted(:last-child) {
|
|
85
|
+
border-radius: var(
|
|
86
|
+
--m3e-segmented-button-end-shape,
|
|
87
|
+
${DesignToken.shape.corner.none} ${DesignToken.shape.corner.full} ${DesignToken.shape.corner.full}
|
|
88
|
+
${DesignToken.shape.corner.none}
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
::slotted(:not(:first-child)) {
|
|
92
|
+
--_segmented-button-left-border: none;
|
|
93
|
+
}
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
/** @internal */
|
|
97
|
+
readonly [selectionManager] = new SelectionManager<M3eButtonSegmentElement>()
|
|
98
|
+
.withWrap()
|
|
99
|
+
.onActiveItemChange(() => this[selectionManager].activeItem?.click());
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Whether multiple options can be selected.
|
|
103
|
+
* @default false
|
|
104
|
+
*/
|
|
105
|
+
@property({ type: Boolean }) multi = false;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Whether to hide the selection indicator.
|
|
109
|
+
* @default false
|
|
110
|
+
*/
|
|
111
|
+
@property({ attribute: "hide-selection-indicator", type: Boolean }) hideSelectionIndicator = false;
|
|
112
|
+
|
|
113
|
+
/** The segments of the button. */
|
|
114
|
+
get segments(): readonly M3eButtonSegmentElement[] {
|
|
115
|
+
return this[selectionManager]?.items ?? [];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The selected segment(s) of the button. */
|
|
119
|
+
get selected(): readonly M3eButtonSegmentElement[] {
|
|
120
|
+
return this[selectionManager]?.selectedItems ?? [];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The selected value(s) of the button. */
|
|
124
|
+
get value(): string | readonly string[] | null {
|
|
125
|
+
const values = this.selected.map((x) => x.value);
|
|
126
|
+
switch (values.length) {
|
|
127
|
+
case 0:
|
|
128
|
+
return null;
|
|
129
|
+
case 1:
|
|
130
|
+
return values[0];
|
|
131
|
+
default:
|
|
132
|
+
return values;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** @inheritdoc @internal */
|
|
137
|
+
override get [formValue]() {
|
|
138
|
+
const values = this.value;
|
|
139
|
+
if (Array.isArray(values)) {
|
|
140
|
+
const data = new FormData();
|
|
141
|
+
for (const value of values) {
|
|
142
|
+
data.append(this.name, value);
|
|
143
|
+
}
|
|
144
|
+
return data;
|
|
145
|
+
}
|
|
146
|
+
return <string | null>values;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** @inheritdoc */
|
|
150
|
+
override connectedCallback(): void {
|
|
151
|
+
super.connectedCallback();
|
|
152
|
+
this[selectionManager].multi = this.multi;
|
|
153
|
+
this[selectionManager].disableRovingTabIndex(this.multi);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** @inheritdoc */
|
|
157
|
+
protected override update(changedProperties: PropertyValues<this>): void {
|
|
158
|
+
super.update(changedProperties);
|
|
159
|
+
|
|
160
|
+
if (changedProperties.has("disabled") && (changedProperties.get("disabled") !== undefined || this.disabled)) {
|
|
161
|
+
this[selectionManager].disabled = this.disabled;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (changedProperties.has("multi")) {
|
|
165
|
+
this.role = this.multi ? "group" : "radiogroup";
|
|
166
|
+
this[selectionManager].multi = this.multi;
|
|
167
|
+
this[selectionManager].disableRovingTabIndex(this.multi);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (changedProperties.has("hideSelectionIndicator")) {
|
|
171
|
+
this.segments.forEach((x) => x.classList.toggle("-hide-selection", this.hideSelectionIndicator));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** @inheritdoc */
|
|
176
|
+
protected override render(): unknown {
|
|
177
|
+
return html`<slot
|
|
178
|
+
@slotchange="${this.#handleSlotChange}"
|
|
179
|
+
@keydown="${this.#handleKeyDown}"
|
|
180
|
+
@change="${this.#handleChange}"
|
|
181
|
+
></slot>`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** @private */
|
|
185
|
+
#handleSlotChange() {
|
|
186
|
+
const { added } = this[selectionManager].setItems([...this.querySelectorAll("m3e-button-segment")]);
|
|
187
|
+
added.forEach((x) => x.classList.toggle("-hide-selection", this.hideSelectionIndicator));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** @private */
|
|
191
|
+
#handleKeyDown(e: KeyboardEvent): void {
|
|
192
|
+
if (!this.multi) {
|
|
193
|
+
this[selectionManager].onKeyDown(e);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** @private */
|
|
198
|
+
#handleChange(e: Event): void {
|
|
199
|
+
e.stopPropagation();
|
|
200
|
+
this.dispatchEvent(new Event("change", { bubbles: true }));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
declare global {
|
|
205
|
+
interface HTMLElementTagNameMap {
|
|
206
|
+
"m3e-segmented-button": M3eSegmentedButtonElement;
|
|
207
|
+
}
|
|
208
|
+
}
|
package/src/index.ts
ADDED