@c2n/switch 0.0.6 → 0.0.8
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/dist/switch.js +73 -4
- package/package.json +18 -6
- package/react.d.ts +24 -0
- package/react.js +3 -0
- package/types/src/switch.d.ts +36 -0
- package/types/src/switch.d.ts.map +1 -1
- package/vue.d.ts +36 -0
- package/vue.js +3 -0
package/dist/switch.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { LitElement, html, unsafeCSS } from "lit";
|
|
2
|
-
import {
|
|
2
|
+
import { property, query, state } from "lit/decorators.js";
|
|
3
|
+
import { customElement } from "@c2n/core/element-helper.js";
|
|
3
4
|
import { classMap } from "lit/directives/class-map.js";
|
|
4
5
|
import { ifDefined } from "lit/directives/if-defined.js";
|
|
5
6
|
import { redispatchEvent } from "@c2n/core/dom-helper.js";
|
|
@@ -18,24 +19,76 @@ function __decorate(decorators, target, key, desc) {
|
|
|
18
19
|
var Switch = class Switch extends LitElement {
|
|
19
20
|
constructor(..._args) {
|
|
20
21
|
super(..._args);
|
|
22
|
+
this.internals = this.attachInternals();
|
|
23
|
+
this.customValidityMessage = "";
|
|
24
|
+
this.disabledByForm = false;
|
|
25
|
+
this.defaultChecked = false;
|
|
26
|
+
this.defaultCheckedCaptured = false;
|
|
21
27
|
this.checked = false;
|
|
22
28
|
this.disabled = false;
|
|
23
29
|
this.name = "";
|
|
24
30
|
this.value = "on";
|
|
31
|
+
this.required = false;
|
|
25
32
|
this.label = "";
|
|
26
33
|
this.hasDescription = false;
|
|
27
34
|
this.dragging = false;
|
|
28
35
|
this.suppressClick = false;
|
|
29
36
|
}
|
|
37
|
+
static {
|
|
38
|
+
this.formAssociated = true;
|
|
39
|
+
}
|
|
30
40
|
static {
|
|
31
41
|
this.styles = unsafeCSS(switch_default);
|
|
32
42
|
}
|
|
43
|
+
connectedCallback() {
|
|
44
|
+
super.connectedCallback();
|
|
45
|
+
if (!this.defaultCheckedCaptured) {
|
|
46
|
+
this.defaultChecked = this.hasAttribute("checked");
|
|
47
|
+
this.defaultCheckedCaptured = true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** The containing form, when this control is associated with one. */
|
|
51
|
+
get form() {
|
|
52
|
+
return this.internals.form;
|
|
53
|
+
}
|
|
54
|
+
/** Labels associated with this form control. */
|
|
55
|
+
get labels() {
|
|
56
|
+
return this.internals.labels;
|
|
57
|
+
}
|
|
58
|
+
get validity() {
|
|
59
|
+
return this.internals.validity;
|
|
60
|
+
}
|
|
61
|
+
get validationMessage() {
|
|
62
|
+
return this.internals.validationMessage;
|
|
63
|
+
}
|
|
64
|
+
get willValidate() {
|
|
65
|
+
return this.internals.willValidate;
|
|
66
|
+
}
|
|
67
|
+
formResetCallback() {
|
|
68
|
+
this.checked = this.defaultChecked;
|
|
69
|
+
}
|
|
70
|
+
formDisabledCallback(disabled) {
|
|
71
|
+
this.disabledByForm = disabled && !this.hasAttribute("disabled");
|
|
72
|
+
}
|
|
73
|
+
formStateRestoreCallback(state) {
|
|
74
|
+
this.checked = state === "checked";
|
|
75
|
+
}
|
|
76
|
+
checkValidity() {
|
|
77
|
+
return this.internals.checkValidity();
|
|
78
|
+
}
|
|
79
|
+
reportValidity() {
|
|
80
|
+
return this.internals.reportValidity();
|
|
81
|
+
}
|
|
82
|
+
setCustomValidity(message) {
|
|
83
|
+
this.customValidityMessage = message;
|
|
84
|
+
this.requestUpdate();
|
|
85
|
+
}
|
|
33
86
|
focus(options) {
|
|
34
87
|
this.formElement?.focus(options);
|
|
35
88
|
}
|
|
36
89
|
/** Flips the state (or forces it) and fires `change`, as a click would. */
|
|
37
90
|
toggle(force) {
|
|
38
|
-
if (this.
|
|
91
|
+
if (this.effectiveDisabled) return;
|
|
39
92
|
const next = force ?? !this.checked;
|
|
40
93
|
if (next === this.checked) return;
|
|
41
94
|
this.checked = next;
|
|
@@ -53,6 +106,16 @@ var Switch = class Switch extends LitElement {
|
|
|
53
106
|
if (changed.has("checked") && this.formElement) this.formElement.checked = this.checked;
|
|
54
107
|
super.update(changed);
|
|
55
108
|
}
|
|
109
|
+
updated(_changed) {
|
|
110
|
+
this.internals.setFormValue(this.checked ? this.value : null, this.checked ? "checked" : "unchecked");
|
|
111
|
+
const missing = this.required && !this.checked;
|
|
112
|
+
const flags = this.customValidityMessage ? { customError: true } : missing ? { valueMissing: true } : {};
|
|
113
|
+
const message = this.customValidityMessage || (missing ? "Please turn on this switch." : "");
|
|
114
|
+
this.internals.setValidity(flags, message, this.formElement);
|
|
115
|
+
}
|
|
116
|
+
get effectiveDisabled() {
|
|
117
|
+
return this.disabled || this.disabledByForm;
|
|
118
|
+
}
|
|
56
119
|
handleDescriptionChange(event) {
|
|
57
120
|
this.updateDescription(event.target);
|
|
58
121
|
}
|
|
@@ -67,7 +130,7 @@ var Switch = class Switch extends LitElement {
|
|
|
67
130
|
return getComputedStyle(this).direction === "rtl";
|
|
68
131
|
}
|
|
69
132
|
handlePointerDown(event) {
|
|
70
|
-
if (this.
|
|
133
|
+
if (this.effectiveDisabled || event.button !== 0) return;
|
|
71
134
|
const track = this.trackElement;
|
|
72
135
|
const padding = parseFloat(getComputedStyle(track).paddingLeft) || 0;
|
|
73
136
|
const travel = Math.max(0, track.clientWidth - 2 * padding - this.thumbElement.getBoundingClientRect().height);
|
|
@@ -141,7 +204,8 @@ var Switch = class Switch extends LitElement {
|
|
|
141
204
|
aria-labelledby=${ifDefined(this.ariaLabelledBy)}
|
|
142
205
|
aria-describedby=${ifDefined(this.ariaDescribedBy)}
|
|
143
206
|
?checked=${this.checked}
|
|
144
|
-
?disabled=${this.
|
|
207
|
+
?disabled=${this.effectiveDisabled}
|
|
208
|
+
?required=${this.required}
|
|
145
209
|
@change=${this.handleChange}
|
|
146
210
|
@click=${this.handleClick}
|
|
147
211
|
@pointerdown=${this.handlePointerDown}
|
|
@@ -166,6 +230,7 @@ var Switch = class Switch extends LitElement {
|
|
|
166
230
|
`;
|
|
167
231
|
}
|
|
168
232
|
};
|
|
233
|
+
__decorate([state()], Switch.prototype, "disabledByForm", void 0);
|
|
169
234
|
__decorate([query("input")], Switch.prototype, "formElement", void 0);
|
|
170
235
|
__decorate([query(".c2-switch-track")], Switch.prototype, "trackElement", void 0);
|
|
171
236
|
__decorate([query(".c2-switch-thumb")], Switch.prototype, "thumbElement", void 0);
|
|
@@ -179,6 +244,10 @@ __decorate([property({
|
|
|
179
244
|
})], Switch.prototype, "disabled", void 0);
|
|
180
245
|
__decorate([property()], Switch.prototype, "name", void 0);
|
|
181
246
|
__decorate([property()], Switch.prototype, "value", void 0);
|
|
247
|
+
__decorate([property({
|
|
248
|
+
type: Boolean,
|
|
249
|
+
reflect: true
|
|
250
|
+
})], Switch.prototype, "required", void 0);
|
|
182
251
|
__decorate([property()], Switch.prototype, "label", void 0);
|
|
183
252
|
__decorate([property({
|
|
184
253
|
type: String,
|
package/package.json
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c2n/switch",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/switch.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"types": "./types/src/switch.d.ts",
|
|
9
|
+
"default": "./dist/switch.js"
|
|
10
|
+
},
|
|
11
|
+
"./react": {
|
|
12
|
+
"types": "./react.d.ts",
|
|
13
|
+
"default": "./react.js"
|
|
14
|
+
},
|
|
15
|
+
"./vue": {
|
|
16
|
+
"types": "./vue.d.ts",
|
|
17
|
+
"default": "./vue.js"
|
|
10
18
|
},
|
|
11
19
|
"./custom-elements.json": "./custom-elements.json"
|
|
12
20
|
},
|
|
13
21
|
"files": [
|
|
14
22
|
"dist",
|
|
15
|
-
"types"
|
|
23
|
+
"types",
|
|
24
|
+
"react.d.ts",
|
|
25
|
+
"react.js",
|
|
26
|
+
"vue.d.ts",
|
|
27
|
+
"vue.js"
|
|
16
28
|
],
|
|
17
29
|
"keywords": [
|
|
18
30
|
"switch",
|
|
@@ -50,12 +62,12 @@
|
|
|
50
62
|
}
|
|
51
63
|
},
|
|
52
64
|
"dependencies": {
|
|
53
|
-
"@c2n/core": "0.0.
|
|
65
|
+
"@c2n/core": "0.0.8",
|
|
54
66
|
"lit": "3.3.3"
|
|
55
67
|
},
|
|
56
68
|
"devDependencies": {
|
|
57
69
|
"@c2n/config": "*"
|
|
58
70
|
},
|
|
59
71
|
"customElements": "custom-elements.json",
|
|
60
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "be59cbccee1d33ca248e39f69b05c0b595ec6c6d"
|
|
61
73
|
}
|
package/react.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// GENERATED by @c2n/framework-types. Do not edit by hand.
|
|
2
|
+
//
|
|
3
|
+
// JSX types for the custom elements of @c2n/switch. Import once, anywhere in the program:
|
|
4
|
+
//
|
|
5
|
+
// import '@c2n/switch/react'
|
|
6
|
+
//
|
|
7
|
+
// Register the elements at module scope before React renders, or React writes an object prop as an
|
|
8
|
+
// attribute and it stringifies.
|
|
9
|
+
|
|
10
|
+
import type { DetailedHTMLProps, HTMLAttributes } from 'react'
|
|
11
|
+
import type { Switch } from '@c2n/switch'
|
|
12
|
+
|
|
13
|
+
/** Standard React host-element attributes plus the element's own public properties. */
|
|
14
|
+
type C2Props<T> = DetailedHTMLProps<HTMLAttributes<T>, T> & Partial<Omit<T, keyof HTMLElement>>
|
|
15
|
+
|
|
16
|
+
declare module 'react' {
|
|
17
|
+
namespace JSX {
|
|
18
|
+
interface IntrinsicElements {
|
|
19
|
+
'c2-switch': C2Props<Switch>
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export {}
|
package/react.js
ADDED
package/types/src/switch.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { LitElement, type PropertyValues } from 'lit';
|
|
2
|
+
import type { TypedAddEventListener, TypedRemoveEventListener } from '@c2n/core/event-helper.js';
|
|
3
|
+
/** Events fired by {@link Switch}, keyed for `addEventListener`. */
|
|
4
|
+
export interface SwitchEventMap {
|
|
5
|
+
change: Event;
|
|
6
|
+
}
|
|
7
|
+
export interface Switch {
|
|
8
|
+
addEventListener: TypedAddEventListener<Switch, SwitchEventMap>;
|
|
9
|
+
removeEventListener: TypedRemoveEventListener<Switch, SwitchEventMap>;
|
|
10
|
+
}
|
|
2
11
|
/**
|
|
3
12
|
* On/off toggle built on a native `<input type="checkbox" role="switch">`, so keyboard activation (Space), form value
|
|
4
13
|
* and the `switch` role come from the browser. The thumb can also be dragged: it follows the pointer and the side it is
|
|
@@ -54,7 +63,13 @@ import { LitElement, type PropertyValues } from 'lit';
|
|
|
54
63
|
* @cssproperty {time} [--c2-switch--transition-duration=200ms] - Thumb slide and colour change.
|
|
55
64
|
*/
|
|
56
65
|
export declare class Switch extends LitElement {
|
|
66
|
+
static formAssociated: boolean;
|
|
57
67
|
static styles: import("lit").CSSResult;
|
|
68
|
+
private readonly internals;
|
|
69
|
+
private customValidityMessage;
|
|
70
|
+
private disabledByForm;
|
|
71
|
+
private defaultChecked;
|
|
72
|
+
private defaultCheckedCaptured;
|
|
58
73
|
protected formElement: HTMLInputElement;
|
|
59
74
|
private trackElement;
|
|
60
75
|
private thumbElement;
|
|
@@ -66,10 +81,15 @@ export declare class Switch extends LitElement {
|
|
|
66
81
|
name: string;
|
|
67
82
|
/** Form value submitted while on. Defaults to `on`, like a native checkbox. */
|
|
68
83
|
value: string;
|
|
84
|
+
/** Requires the switch to be on for the form to be valid. */
|
|
85
|
+
required: boolean;
|
|
69
86
|
/** Label text when the default slot is empty. */
|
|
70
87
|
label: string;
|
|
88
|
+
/** Accessible name used when no visible label names the switch. */
|
|
71
89
|
ariaLabel: string;
|
|
90
|
+
/** Id of the element that labels the switch. */
|
|
72
91
|
ariaLabelledBy: undefined | string;
|
|
92
|
+
/** Id of the element that describes the switch. */
|
|
73
93
|
ariaDescribedBy: undefined | string;
|
|
74
94
|
private hasDescription;
|
|
75
95
|
/** True while the thumb follows the pointer; disables the slide transition. */
|
|
@@ -77,12 +97,28 @@ export declare class Switch extends LitElement {
|
|
|
77
97
|
private drag?;
|
|
78
98
|
/** The click that follows a drag must not toggle the input again. */
|
|
79
99
|
private suppressClick;
|
|
100
|
+
connectedCallback(): void;
|
|
101
|
+
/** The containing form, when this control is associated with one. */
|
|
102
|
+
get form(): HTMLFormElement | null;
|
|
103
|
+
/** Labels associated with this form control. */
|
|
104
|
+
get labels(): NodeList;
|
|
105
|
+
get validity(): ValidityState;
|
|
106
|
+
get validationMessage(): string;
|
|
107
|
+
get willValidate(): boolean;
|
|
108
|
+
formResetCallback(): void;
|
|
109
|
+
formDisabledCallback(disabled: boolean): void;
|
|
110
|
+
formStateRestoreCallback(state: string | File | FormData | null): void;
|
|
111
|
+
checkValidity(): boolean;
|
|
112
|
+
reportValidity(): boolean;
|
|
113
|
+
setCustomValidity(message: string): void;
|
|
80
114
|
focus(options?: FocusOptions): void;
|
|
81
115
|
/** Flips the state (or forces it) and fires `change`, as a click would. */
|
|
82
116
|
toggle(force?: boolean): void;
|
|
83
117
|
/** `slotchange` does not fire for server-rendered slots, so read the description once after the first render. */
|
|
84
118
|
firstUpdated(): void;
|
|
85
119
|
protected update(changed: PropertyValues<this>): void;
|
|
120
|
+
protected updated(_changed: PropertyValues<this>): void;
|
|
121
|
+
private get effectiveDisabled();
|
|
86
122
|
private handleDescriptionChange;
|
|
87
123
|
private updateDescription;
|
|
88
124
|
private handleChange;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../src/switch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAmB,KAAK,cAAc,EAAE,MAAM,KAAK,CAAA;
|
|
1
|
+
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../src/switch.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. Do not edit by hand.
|
|
2
|
+
//
|
|
3
|
+
// Vue template types for the custom elements of @c2n/switch. Import once, anywhere in the program:
|
|
4
|
+
//
|
|
5
|
+
// import '@c2n/switch/vue'
|
|
6
|
+
//
|
|
7
|
+
// Tell the compiler about the tags as well, or every c2-* tag is resolved as a Vue component and renders
|
|
8
|
+
// nothing: template.compilerOptions.isCustomElement = (tag) => tag.startsWith('c2-') in vite.config.ts.
|
|
9
|
+
|
|
10
|
+
import type { DefineComponent, HTMLAttributes } from 'vue'
|
|
11
|
+
import type { Switch, SwitchEventMap } from '@c2n/switch'
|
|
12
|
+
|
|
13
|
+
/** The element's own public properties, plus every attribute Vue understands on a host element. */
|
|
14
|
+
type C2Props<T> = Partial<Omit<T, keyof HTMLElement>> & HTMLAttributes
|
|
15
|
+
|
|
16
|
+
declare module 'vue' {
|
|
17
|
+
interface GlobalComponents {
|
|
18
|
+
'c2-switch': DefineComponent<
|
|
19
|
+
C2Props<Switch> & {
|
|
20
|
+
'aria-label'?: unknown
|
|
21
|
+
'aria-labelledby'?: unknown
|
|
22
|
+
'aria-describedby'?: unknown
|
|
23
|
+
onChange?: (event: SwitchEventMap['change']) => void
|
|
24
|
+
}
|
|
25
|
+
>
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare module '@vue/runtime-dom' {
|
|
30
|
+
interface HTMLAttributes {
|
|
31
|
+
/** Vue's own definition omits it, and slotting a plain element into a component needs it. */
|
|
32
|
+
slot?: string
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export {}
|
package/vue.js
ADDED