@c2n/label 0.0.6
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 +21 -0
- package/README.md +3 -0
- package/dist/label.js +90 -0
- package/package.json +59 -0
- package/types/src/label.d.ts +53 -0
- package/types/src/label.d.ts.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Nguyen Thai Vinh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/label.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { LitElement, html, nothing, unsafeCSS } from "lit";
|
|
2
|
+
import { customElement, property } from "lit/decorators.js";
|
|
3
|
+
//#region src/label.scss?inline
|
|
4
|
+
var label_default = "/* ex : var((width: 24px), width, c2-checkbox) returns var(--c2-checkbox-width, 24px) */\n:host {\n display: inline-flex;\n}\n\n:host([hidden]) {\n display: none;\n}\n\n:host([for]:not([disabled])) {\n cursor: pointer;\n}\n\n:host([disabled]) {\n cursor: default;\n}\n\n.c2-label {\n display: inline-flex;\n align-items: baseline;\n gap: var(--c2-label__container--gap,4px);\n color: var(--c2-label__container--color,inherit);\n font-family: var(--c2-label__container--font-family,inherit);\n font-size: var(--c2-label__container--font-size,inherit);\n font-weight: var(--c2-label__container--font-weight,inherit);\n line-height: var(--c2-label__container--line-height,inherit);\n letter-spacing: var(--c2-label__container--letter-spacing,inherit);\n text-transform: var(--c2-label__container--text-transform,inherit);\n transition: color 250ms cubic-bezier(0.2, 0, 0, 1);\n}\n\n:host([for]:not([disabled])) .c2-label:hover {\n color: var(--c2-label__container__hover--color,var(--c2-label__container--color,inherit));\n}\n\n:host([disabled]) .c2-label {\n opacity: var(--c2-label__container__disabled--opacity,0.38);\n}\n\n.c2-label__required {\n color: var(--c2-label__required-indicator--color,rgb(211, 21, 16));\n font-size: var(--c2-label__required-indicator--font-size,inherit);\n font-weight: var(--c2-label__required-indicator--font-weight,inherit);\n line-height: 1;\n}";
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region \0@oxc-project+runtime@0.148.0/helpers/esm/decorate.js
|
|
7
|
+
function __decorate(decorators, target, key, desc) {
|
|
8
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
10
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
11
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/label.ts
|
|
15
|
+
var labelCount = 0;
|
|
16
|
+
var Label = class Label extends LitElement {
|
|
17
|
+
static {
|
|
18
|
+
this.styles = unsafeCSS(label_default);
|
|
19
|
+
}
|
|
20
|
+
constructor() {
|
|
21
|
+
super();
|
|
22
|
+
this.disabled = false;
|
|
23
|
+
this.required = false;
|
|
24
|
+
this.for = "";
|
|
25
|
+
this.linkedTarget = null;
|
|
26
|
+
this.handleClick = (event) => {
|
|
27
|
+
if (this.disabled || event.defaultPrevented) return;
|
|
28
|
+
const target = this.resolveTarget();
|
|
29
|
+
if (!target) return;
|
|
30
|
+
const toggle = (el) => el instanceof HTMLInputElement && (el.type === "checkbox" || el.type === "radio");
|
|
31
|
+
if (toggle(target)) {
|
|
32
|
+
target.click();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const inner = target.shadowRoot?.querySelector("input[type=\"checkbox\"], input[type=\"radio\"]");
|
|
36
|
+
if (toggle(inner)) {
|
|
37
|
+
inner.click();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
target.focus();
|
|
41
|
+
};
|
|
42
|
+
this.addEventListener("click", this.handleClick);
|
|
43
|
+
}
|
|
44
|
+
disconnectedCallback() {
|
|
45
|
+
super.disconnectedCallback();
|
|
46
|
+
this.unlinkTarget();
|
|
47
|
+
}
|
|
48
|
+
updated(changed) {
|
|
49
|
+
super.updated(changed);
|
|
50
|
+
if (changed.has("for")) this.linkTarget();
|
|
51
|
+
}
|
|
52
|
+
resolveTarget() {
|
|
53
|
+
if (!this.for) return null;
|
|
54
|
+
const root = this.getRootNode();
|
|
55
|
+
return (typeof root.getElementById === "function" ? root.getElementById(this.for) : null) ?? this.parentElement?.querySelector(`#${CSS.escape(this.for)}`) ?? null;
|
|
56
|
+
}
|
|
57
|
+
/** Points the target's `aria-labelledby` at this label (unless it already has one) so the label names the control. */
|
|
58
|
+
linkTarget() {
|
|
59
|
+
this.unlinkTarget();
|
|
60
|
+
const target = this.resolveTarget();
|
|
61
|
+
if (!target || target.hasAttribute("aria-labelledby")) return;
|
|
62
|
+
if (!this.id) this.id = `c2-label-${++labelCount}`;
|
|
63
|
+
target.setAttribute("aria-labelledby", this.id);
|
|
64
|
+
this.linkedTarget = target;
|
|
65
|
+
}
|
|
66
|
+
unlinkTarget() {
|
|
67
|
+
if (this.linkedTarget?.getAttribute("aria-labelledby") === this.id) this.linkedTarget.removeAttribute("aria-labelledby");
|
|
68
|
+
this.linkedTarget = null;
|
|
69
|
+
}
|
|
70
|
+
render() {
|
|
71
|
+
return html`<span class="c2-label" part="label"
|
|
72
|
+
><slot></slot>${this.required ? html`<span class="c2-label__required" part="required-indicator" aria-hidden="true"><slot name="required-indicator">*</slot></span>` : nothing}</span
|
|
73
|
+
>`;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
__decorate([property({
|
|
77
|
+
type: Boolean,
|
|
78
|
+
reflect: true
|
|
79
|
+
})], Label.prototype, "disabled", void 0);
|
|
80
|
+
__decorate([property({
|
|
81
|
+
type: Boolean,
|
|
82
|
+
reflect: true
|
|
83
|
+
})], Label.prototype, "required", void 0);
|
|
84
|
+
__decorate([property({
|
|
85
|
+
type: String,
|
|
86
|
+
reflect: true
|
|
87
|
+
})], Label.prototype, "for", void 0);
|
|
88
|
+
Label = __decorate([customElement("c2-label")], Label);
|
|
89
|
+
//#endregion
|
|
90
|
+
export { Label };
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@c2n/label",
|
|
3
|
+
"version": "0.0.6",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/label.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"default": "./dist/label.js",
|
|
9
|
+
"types": "./types/src/label.d.ts"
|
|
10
|
+
},
|
|
11
|
+
"./custom-elements.json": "./custom-elements.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"types"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"label",
|
|
19
|
+
"web component",
|
|
20
|
+
"lit"
|
|
21
|
+
],
|
|
22
|
+
"author": "code2nguyen@gmail.com",
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"registry": "https://registry.npmjs.org",
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/code2nguyen/web-components.git"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"dev": "vite",
|
|
33
|
+
"build": "wireit",
|
|
34
|
+
"type-check": "wireit"
|
|
35
|
+
},
|
|
36
|
+
"wireit": {
|
|
37
|
+
"type-check": {
|
|
38
|
+
"dependencies": [
|
|
39
|
+
"../../core:build"
|
|
40
|
+
],
|
|
41
|
+
"command": "tsc -p tsconfig.lib.json --composite false"
|
|
42
|
+
},
|
|
43
|
+
"build": {
|
|
44
|
+
"dependencies": [
|
|
45
|
+
"type-check"
|
|
46
|
+
],
|
|
47
|
+
"command": "vite build"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@c2n/core": "0.0.6",
|
|
52
|
+
"lit": "3.3.3"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@c2n/config": "*"
|
|
56
|
+
},
|
|
57
|
+
"customElements": "custom-elements.json",
|
|
58
|
+
"gitHead": "2921054bc75299da66dad11c1e374246ff88a51e"
|
|
59
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { LitElement, type PropertyValues } from 'lit';
|
|
2
|
+
/**
|
|
3
|
+
* Caption for a form control. Clicking it activates the element referenced by `for`: native checkboxes and radios
|
|
4
|
+
* (also the ones inside a component's shadow root, such as `c2-checkbox`) are toggled, anything else is focused.
|
|
5
|
+
* It also names that element for assistive technology by setting `aria-labelledby` when the target has none.
|
|
6
|
+
* Typography inherits from the surrounding text until a `--c2-label__container--*` variable is set.
|
|
7
|
+
*
|
|
8
|
+
* @tag c2-label
|
|
9
|
+
*
|
|
10
|
+
* @slot default - Label text.
|
|
11
|
+
* @slot required-indicator - Marker shown after the text when `required` is set. Defaults to an asterisk.
|
|
12
|
+
*
|
|
13
|
+
* @cssproperty {pixel} [--c2-label__container--gap=4px]
|
|
14
|
+
* @cssproperty {color} [--c2-label__container--color=inherit]
|
|
15
|
+
* @cssproperty {font-family} [--c2-label__container--font-family=inherit]
|
|
16
|
+
* @cssproperty {pixel} [--c2-label__container--font-size=inherit]
|
|
17
|
+
* @cssproperty {font-weight} [--c2-label__container--font-weight=inherit]
|
|
18
|
+
* @cssproperty {line-height} [--c2-label__container--line-height=inherit]
|
|
19
|
+
* @cssproperty {pixel} [--c2-label__container--letter-spacing=inherit]
|
|
20
|
+
* @cssproperty {text-transform} [--c2-label__container--text-transform=inherit]
|
|
21
|
+
*
|
|
22
|
+
* @cssproperty {color} --c2-label__container__hover--color
|
|
23
|
+
* @cssproperty {opacity} [--c2-label__container__disabled--opacity=0.38]
|
|
24
|
+
*
|
|
25
|
+
* @cssproperty {color} [--c2-label__required-indicator--color=rgb(211, 21, 16)]
|
|
26
|
+
* @cssproperty {pixel} [--c2-label__required-indicator--font-size=inherit]
|
|
27
|
+
* @cssproperty {font-weight} [--c2-label__required-indicator--font-weight=inherit]
|
|
28
|
+
*/
|
|
29
|
+
export declare class Label extends LitElement {
|
|
30
|
+
static styles: import("lit").CSSResult;
|
|
31
|
+
/** Disables the label: clicks no longer reach the control and the text is dimmed. */
|
|
32
|
+
disabled: boolean;
|
|
33
|
+
/** Shows the required indicator after the text. */
|
|
34
|
+
required: boolean;
|
|
35
|
+
/** `id` of the control this label describes. It is looked up in the label's own root, then among its siblings. */
|
|
36
|
+
for: string;
|
|
37
|
+
private linkedTarget;
|
|
38
|
+
constructor();
|
|
39
|
+
disconnectedCallback(): void;
|
|
40
|
+
protected updated(changed: PropertyValues<this>): void;
|
|
41
|
+
private resolveTarget;
|
|
42
|
+
/** Points the target's `aria-labelledby` at this label (unless it already has one) so the label names the control. */
|
|
43
|
+
private linkTarget;
|
|
44
|
+
private unlinkTarget;
|
|
45
|
+
private handleClick;
|
|
46
|
+
render(): import("lit-html").TemplateResult<1>;
|
|
47
|
+
}
|
|
48
|
+
declare global {
|
|
49
|
+
interface HTMLElementTagNameMap {
|
|
50
|
+
'c2-label': Label;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=label.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"label.d.ts","sourceRoot":"","sources":["../../src/label.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA4B,KAAK,cAAc,EAAE,MAAM,KAAK,CAAA;AAM/E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBACa,KAAM,SAAQ,UAAU;IACnC,OAAgB,MAAM,0BAAoB;IAE1C,qFAAqF;IACzC,QAAQ,UAAQ;IAE5D,mDAAmD;IACP,QAAQ,UAAQ;IAE5D,kHAAkH;IACvE,GAAG,SAAK;IAEnD,OAAO,CAAC,YAAY,CAA2B;;IAOtC,oBAAoB,IAAI,IAAI;cAKlB,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC,GAAG,IAAI;IAK/D,OAAO,CAAC,aAAa;IAOrB,sHAAsH;IACtH,OAAO,CAAC,UAAU;IASlB,OAAO,CAAC,YAAY;IAOpB,OAAO,CAAC,WAAW,CAgBlB;IAEQ,MAAM;CAShB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,UAAU,EAAE,KAAK,CAAA;KAClB;CACF"}
|