@m3e/radio-group 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.
@@ -0,0 +1,257 @@
1
+ import { css, CSSResultGroup, html, LitElement, PropertyValues } 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
+ Focusable,
11
+ HoverController,
12
+ KeyboardClick,
13
+ Labelled,
14
+ M3eFocusRingElement,
15
+ M3eRippleElement,
16
+ M3eStateLayerElement,
17
+ PressedController,
18
+ Role,
19
+ Touched,
20
+ } from "@m3e/core";
21
+
22
+ import { selectionManager } from "@m3e/core/a11y";
23
+
24
+ /**
25
+ * @summary
26
+ * A radio button that allows a user to select one option from a set of options.
27
+ *
28
+ * @description
29
+ * The `m3e-radio` component represents a radio button that enables users to select an options from a set.
30
+ * It supports selection from mutually exclusive options, emitting `input` and `change` events when its state updates.
31
+ * The component reflects its state through customizable CSS properties for hover, focus, ripple, and icon styling—
32
+ * adapting dynamically based on whether it is selected, unselected, or disabled.
33
+ *
34
+ * Attributes like `checked`, `disabled`, and `value` control its behavior and accessibility, while its visual
35
+ * presentation can be tuned via design tokens such as `--m3e-radio-container-size` and `--m3e-radio-icon-size`.
36
+ *
37
+ * @example
38
+ * The following example illustrates using `m3e-radio-group` and `m3e-radio` to present a group of options.
39
+ * ```html
40
+ * <label for="rdg1">Radio group</label>
41
+ * <br />
42
+ * <m3e-radio-group id="rdg1">
43
+ * <label><m3e-radio value="1"></m3e-radio> Value 1</label>
44
+ * <label><m3e-radio value="2"></m3e-radio> Value 2</label>
45
+ * <label><m3e-radio value="3"></m3e-radio> Value 3</label>
46
+ * <label><m3e-radio value="4"></m3e-radio> Value 4</label>
47
+ * </m3e-radio-group>
48
+ * ```
49
+ *
50
+ * @tag m3e-radio
51
+ *
52
+ * @attr checked - Whether the element is checked.
53
+ * @attr disabled - Whether the element is disabled.
54
+ * @attr value - A string representing the value of the radio.
55
+ *
56
+ * @fires input - Emitted when the checked state changes.
57
+ * @fires change - Emitted when the checked state changes.
58
+ *
59
+ * @cssprop --m3e-radio-container-size - Base size of the radio button container.
60
+ * @cssprop --m3e-radio-icon-size - Size of the radio icon inside the wrapper.
61
+ * @cssprop --m3e-radio-unselected-hover-color - Hover state layer color when radio is not selected.
62
+ * @cssprop --m3e-radio-unselected-focus-color - Focus state layer color when radio is not selected.
63
+ * @cssprop --m3e-radio-unselected-ripple-color - Ripple color when radio is not selected.
64
+ * @cssprop --m3e-radio-unselected-icon-color - Icon color when radio is not selected.
65
+ * @cssprop --m3e-radio-selected-hover-color - Hover state layer color when radio is selected.
66
+ * @cssprop --m3e-radio-selected-focus-color - Focus state layer color when radio is selected.
67
+ * @cssprop --m3e-radio-selected-ripple-color - Ripple color when radio is selected.
68
+ * @cssprop --m3e-radio-selected-icon-color - Icon color when radio is selected.
69
+ * @cssprop --m3e-radio-disabled-icon-color - Icon color when radio is disabled.
70
+ */
71
+ @customElement("m3e-radio")
72
+ export class M3eRadioElement extends Dirty(
73
+ Touched(Checked(Labelled(KeyboardClick(Focusable(Disabled(AttachInternals(Role(LitElement, "radio"), true)))))))
74
+ ) {
75
+ /** The styles of the element. */
76
+ static override styles: CSSResultGroup = css`
77
+ :host {
78
+ display: inline-block;
79
+ outline: none;
80
+ width: fit-content;
81
+ height: fit-content;
82
+ vertical-align: middle;
83
+ }
84
+ :host(:not(:disabled)) {
85
+ cursor: pointer;
86
+ }
87
+ .base {
88
+ box-sizing: border-box;
89
+ vertical-align: middle;
90
+ display: inline-flex;
91
+ align-items: center;
92
+ justify-content: center;
93
+ position: relative;
94
+ border-radius: 50%;
95
+ width: calc(var(--m3e-radio-container-size, 2.5rem) + ${DesignToken.density.calc(-3)});
96
+ height: calc(var(--m3e-radio-container-size, 2.5rem) + ${DesignToken.density.calc(-3)});
97
+ }
98
+ .touch {
99
+ position: absolute;
100
+ height: 3rem;
101
+ width: 3rem;
102
+ margin: auto;
103
+ }
104
+ .wrapper {
105
+ box-sizing: border-box;
106
+ pointer-events: none;
107
+ width: var(--m3e-radio-icon-size, 1.25rem);
108
+ height: var(--m3e-radio-icon-size, 1.25rem);
109
+ }
110
+ .circle {
111
+ fill: currentColor;
112
+ }
113
+ :host(:not([checked])) .circle.inner {
114
+ opacity: 0;
115
+ }
116
+ :host(:not([checked])) .base {
117
+ --m3e-state-layer-hover-color: var(--m3e-radio-unselected-hover-color, ${DesignToken.color.onSurface});
118
+ --m3e-state-layer-focus-color: var(--m3e-radio-unselected-focus-color, ${DesignToken.color.onSurface});
119
+ --m3e-ripple-color: var(--m3e-radio-unselected-ripple-color, ${DesignToken.color.onSurface});
120
+ color: var(--m3e-radio-unselected-icon-color, ${DesignToken.color.onSurfaceVariant});
121
+ }
122
+ :host([checked]) .base {
123
+ --m3e-state-layer-hover-color: var(--m3e-radio-selected-hover-color, ${DesignToken.color.primary});
124
+ --m3e-state-layer-focus-color: var(--m3e-radio-selected-focus-color, ${DesignToken.color.primary});
125
+ --m3e-ripple-color: var(--m3e-radio-selected-ripple-color, ${DesignToken.color.primary});
126
+ color: var(--m3e-radio-selected-icon-color, ${DesignToken.color.primary});
127
+ }
128
+ :host(:disabled) .base {
129
+ color: color-mix(in srgb, var(--m3e-radio-disabled-icon-color, ${DesignToken.color.onSurface}) 38%, transparent);
130
+ }
131
+ @media (forced-colors: active) {
132
+ :host(:not([checked])) .base,
133
+ :host([checked]) .base {
134
+ --m3e-state-layer-hover-color: var(--_radio-forced-color, CanvasText);
135
+ --m3e-state-layer-focus-color: var(--_radio-forced-color, CanvasText);
136
+ --m3e-ripple-color: var(--_radio-forced-color, CanvasText);
137
+ color: var(--_radio-forced-color, CanvasText);
138
+ }
139
+ :host(:disabled) .base {
140
+ color: GrayText;
141
+ }
142
+ }
143
+ `;
144
+
145
+ /** @private */ @query(".focus-ring") private readonly _focusRing?: M3eFocusRingElement;
146
+ /** @private */ @query(".state-layer") private readonly _stateLayer?: M3eStateLayerElement;
147
+ /** @private */ @query(".ripple") private readonly _ripple?: M3eRippleElement;
148
+ /** @private */ readonly #clickHandler = (e: Event) => this.#handleClick(e);
149
+
150
+ /** @private */ readonly #hoverController = new HoverController(this, {
151
+ target: null,
152
+ callback: (hovering) => {
153
+ if (this.disabled) return;
154
+ if (hovering) {
155
+ this._stateLayer?.show("hover");
156
+ } else {
157
+ this._stateLayer?.hide("hover");
158
+ }
159
+ },
160
+ });
161
+
162
+ /** @private */ readonly #pressedController = new PressedController(this, {
163
+ target: null,
164
+ callback: (pressed) => {
165
+ if (this.disabled) return;
166
+ if (pressed) {
167
+ this._ripple?.show(0, 0, true);
168
+ } else {
169
+ this._ripple?.hide();
170
+ }
171
+ },
172
+ });
173
+
174
+ /**
175
+ * A string representing the value of the radio.
176
+ * @default "on"
177
+ */
178
+ @property() value = "on";
179
+
180
+ /** @inheritdoc */
181
+ override connectedCallback(): void {
182
+ super.connectedCallback();
183
+
184
+ this.addEventListener("click", this.#clickHandler);
185
+ for (const label of this.labels) {
186
+ this.#hoverController.observe(label);
187
+ this.#pressedController.observe(label);
188
+ }
189
+ }
190
+
191
+ /** @inheritdoc */
192
+ override disconnectedCallback(): void {
193
+ super.disconnectedCallback();
194
+
195
+ this.removeEventListener("click", this.#clickHandler);
196
+ for (const label of this.labels) {
197
+ this.#hoverController.unobserve(label);
198
+ this.#pressedController.unobserve(label);
199
+ }
200
+ }
201
+
202
+ /** @inheritdoc */
203
+ protected override update(changedProperties: PropertyValues<this>): void {
204
+ super.update(changedProperties);
205
+
206
+ if (changedProperties.has("checked")) {
207
+ this.closest("m3e-radio-group")?.[selectionManager].notifySelectionChange(this);
208
+ }
209
+ }
210
+
211
+ /** @inheritdoc */
212
+ protected override firstUpdated(_changedProperties: PropertyValues<this>): void {
213
+ super.firstUpdated(_changedProperties);
214
+ [this._focusRing, this._stateLayer, this._ripple].forEach((x) => x?.attach(this));
215
+ }
216
+
217
+ /** @inheritdoc */
218
+ protected override render(): unknown {
219
+ return html`<div class="base">
220
+ <m3e-state-layer class="state-layer" ?disabled="${this.disabled}"></m3e-state-layer>
221
+ <m3e-focus-ring class="focus-ring" ?disabled="${this.disabled}"></m3e-focus-ring>
222
+ <m3e-ripple class="ripple" centered disable-enter ?disabled="${this.disabled}"></m3e-ripple>
223
+ <div class="touch" aria-hidden="true"></div>
224
+ <div class="wrapper" aria-hidden="true">${this.#renderIcon()}</div>
225
+ </div>`;
226
+ }
227
+
228
+ /** @private */
229
+ #renderIcon(): unknown {
230
+ return html`<svg viewBox="0 0 20 20">
231
+ <mask id="cutout2">
232
+ <rect width="100%" height="100%" fill="white"></rect>
233
+ <circle cx="10" cy="10" r="8" fill="black"></circle>
234
+ </mask>
235
+ <circle class="outer circle" cx="10" cy="10" r="10" mask="url(#cutout2)"></circle>
236
+ <circle class="inner circle" cx="10" cy="10" r="5"></circle>
237
+ </svg>`;
238
+ }
239
+
240
+ /** @private */
241
+ #handleClick(e: Event): void {
242
+ if (e.defaultPrevented || this.checked) return;
243
+ this.checked = true;
244
+ if (this.dispatchEvent(new Event("input", { bubbles: true, composed: true, cancelable: true }))) {
245
+ this.closest("m3e-radio-group")?.[selectionManager].notifySelectionChange(this);
246
+ this.dispatchEvent(new Event("change", { bubbles: true }));
247
+ } else {
248
+ this.checked = false;
249
+ }
250
+ }
251
+ }
252
+
253
+ declare global {
254
+ interface HTMLElementTagNameMap {
255
+ "m3e-radio": M3eRadioElement;
256
+ }
257
+ }
@@ -0,0 +1,170 @@
1
+ import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit";
2
+ import { customElement } from "lit/decorators.js";
3
+
4
+ import {
5
+ AttachInternals,
6
+ Labelled,
7
+ ConstraintValidation,
8
+ DesignToken,
9
+ Dirty,
10
+ Disabled,
11
+ FormAssociated,
12
+ formValue,
13
+ Required,
14
+ RequiredConstraintValidation,
15
+ Touched,
16
+ Role,
17
+ } from "@m3e/core";
18
+
19
+ import { SelectionManager, selectionManager } from "@m3e/core/a11y";
20
+
21
+ import { M3eRadioElement } from "./RadioElement";
22
+
23
+ /**
24
+ * @summary
25
+ * A container for a set of radio buttons that enforces single selection.
26
+ *
27
+ * @description
28
+ * The `m3e-radio-group` component orchestrates a collection of `m3e-radio` components, ensuring that
29
+ * only one option is selected at a time. It manages selection state, propagates value changes, and
30
+ * supports form integration through `name` and `value` attributes.
31
+ *
32
+ * @example
33
+ * The following example illustrates using `m3e-radio-group` and `m3e-radio` to present a group of options.
34
+ * ```html
35
+ * <label for="rdg1">Radio group</label>
36
+ * <br />
37
+ * <m3e-radio-group id="rdg1">
38
+ * <label><m3e-radio value="1"></m3e-radio> Value 1</label>
39
+ * <label><m3e-radio value="2"></m3e-radio> Value 2</label>
40
+ * <label><m3e-radio value="3"></m3e-radio> Value 3</label>
41
+ * <label><m3e-radio value="4"></m3e-radio> Value 4</label>
42
+ * </m3e-radio-group>
43
+ * ```
44
+ *
45
+ * @tag m3e-radio-group
46
+ *
47
+ * @slot - Renders the radio buttons of the group.
48
+ *
49
+ * @attr disabled - Whether the element is disabled.
50
+ * @attr name - The name that identifies the element when submitting the associated form.
51
+ * @attr required - Whether the element is required.
52
+ *
53
+ * @fires change - Emitted when the checked state of a radio button changes.
54
+ *
55
+ * @cssprop --m3e-radio-error-hover-color - Fallback hover color used when the radio is invalid and touched.
56
+ * @cssprop --m3e-radio-error-focus-color - Fallback focus color used when the radio is invalid and touched.
57
+ * @cssprop --m3e-radio-error-ripple-color - Fallback ripple color used when the radio is invalid and touched.
58
+ * @cssprop --m3e-radio-error-icon-color - Fallback icon color used when the radio is invalid and touched.
59
+ */
60
+ @customElement("m3e-radio-group")
61
+ export class M3eRadioGroupElement extends Labelled(
62
+ RequiredConstraintValidation(
63
+ Dirty(
64
+ Touched(Required(ConstraintValidation(FormAssociated(Disabled(AttachInternals(Role(LitElement, "radiogroup")))))))
65
+ )
66
+ )
67
+ ) {
68
+ /** The styles of the element. */
69
+ static override styles: CSSResultGroup = css`
70
+ :host {
71
+ display: inline;
72
+ }
73
+ :host(.-touched:invalid) {
74
+ --m3e-radio-unselected-hover-color: var(--m3e-radio-error-hover-color, ${DesignToken.color.error});
75
+ --m3e-radio-unselected-focus-color: var(--m3e-radio-error-focus-color, ${DesignToken.color.error});
76
+ --m3e-radio-unselected-ripple-color: var(--m3e-radio-error-ripple-color, ${DesignToken.color.error});
77
+ --m3e-radio-unselected-icon-color: var(--m3e-radio-error-icon-color, ${DesignToken.color.error});
78
+ color: var(--m3e-radio-error-icon-color, ${DesignToken.color.error});
79
+ }
80
+ @media (forced-colors: active) {
81
+ :host(.-touched:invalid) {
82
+ --_radio-forced-color: Highlight;
83
+ color: Highlight;
84
+ }
85
+ }
86
+ `;
87
+
88
+ /** @private */ readonly #focusOutHandler = () => this.checkValidity();
89
+
90
+ /** @internal */
91
+ readonly [selectionManager] = new SelectionManager<M3eRadioElement>().withWrap().onActiveItemChange(() => {
92
+ this[selectionManager].activeItem?.click();
93
+ });
94
+
95
+ /** The radio buttons in the group. */
96
+ get radios(): readonly M3eRadioElement[] {
97
+ return this[selectionManager]?.items ?? [];
98
+ }
99
+
100
+ /** The selected radio. */
101
+ get selected(): M3eRadioElement | null {
102
+ return this[selectionManager]?.selectedItems[0] ?? null;
103
+ }
104
+
105
+ /** The selected value of the radio group. */
106
+ get value(): string | null {
107
+ return this.selected?.value ?? null;
108
+ }
109
+
110
+ /** @inheritdoc @internal */
111
+ override get [formValue](): string | File | FormData | null {
112
+ return this.value;
113
+ }
114
+
115
+ /** @inheritdoc */
116
+ override connectedCallback(): void {
117
+ super.connectedCallback();
118
+ this.addEventListener("focusout", this.#focusOutHandler, { capture: true });
119
+ }
120
+
121
+ /** @inheritdoc */
122
+ override disconnectedCallback(): void {
123
+ super.disconnectedCallback();
124
+ this.removeEventListener("focusout", this.#focusOutHandler, { capture: true });
125
+ }
126
+
127
+ /** @inheritdoc */
128
+ protected override update(changedProperties: PropertyValues<this>): void {
129
+ super.update(changedProperties);
130
+
131
+ if (changedProperties.has("disabled") && (changedProperties.get("disabled") !== undefined || this.disabled)) {
132
+ this[selectionManager].disabled = this.disabled;
133
+ }
134
+ }
135
+
136
+ /** @inheritdoc */
137
+ protected override render(): unknown {
138
+ return html`<slot
139
+ @slotchange="${this.#handleSlotChange}"
140
+ @keydown="${this.#handleKeyDown}"
141
+ @change="${this.#handleChange}"
142
+ ></slot>`;
143
+ }
144
+
145
+ /** @private */
146
+ #handleSlotChange() {
147
+ this[selectionManager].setItems([...this.querySelectorAll("m3e-radio")]);
148
+ }
149
+
150
+ /** @private */
151
+ #handleKeyDown(e: KeyboardEvent): void {
152
+ this[selectionManager].onKeyDown(e);
153
+ }
154
+
155
+ /** @private */
156
+ #handleChange(e: Event): void {
157
+ e.stopPropagation();
158
+ if (this.ariaInvalid === "true") {
159
+ this.checkValidity();
160
+ }
161
+
162
+ this.dispatchEvent(new Event("change", { bubbles: true }));
163
+ }
164
+ }
165
+
166
+ declare global {
167
+ interface HTMLElementTagNameMap {
168
+ "m3e-radio-group": M3eRadioGroupElement;
169
+ }
170
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./RadioElement";
2
+ export * from "./RadioGroupElement";
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist/src"
6
+ },
7
+ "include": ["src/**/*.ts", "**/*.mjs", "**/*.js"],
8
+ "exclude": []
9
+ }