@telperion/elements 4.2.0 → 5.0.0

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,139 @@
1
+ import { LitElement } from 'lit';
2
+
3
+ type PredefinedColors = "contrast" | "primary" | "secondary" | "tertiary" | "quaternary" | "success" | "danger" | "warning" | "neutral";
4
+ type ShadeMixerType = "none" | "black" | "white";
5
+ /**
6
+ * Base class that powers Telperion's shared color/design system.
7
+ *
8
+ * Elements that extend `ShadeMixerLitElement` participate in a fully themable
9
+ * palette driven entirely by CSS custom properties. The class:
10
+ *
11
+ * 1. Auto-injects a default palette onto `:root` — `--tp-color-white`,
12
+ * `--tp-color-black`, `--tp-color-contrast`, `--tp-color-primary`,
13
+ * `--tp-color-secondary`, `--tp-color-tertiary`, `--tp-color-quaternary`,
14
+ * `--tp-color-success`, `--tp-color-danger`, `--tp-color-warning`, and
15
+ * `--tp-color-neutral`. Override any of them at `:root` or any ancestor to
16
+ * re-theme every shade-mixer-aware element underneath.
17
+ * 2. Exposes `shadedElementStyles` — a `css` block that resolves the
18
+ * selected `color` to `--tp-element-color`, applies the `shade`/`mixer`
19
+ * math, and exposes the result as `--tp-calc-element-color` and
20
+ * `--tp-calc-contrast-color` for consumers (variants, hover, focus, …).
21
+ * Subclasses include it once in their own host rule.
22
+ * 3. Provides three reflected attributes that drive the math:
23
+ * - `color` — palette token name (predefined or custom).
24
+ * - `shade` (`0`–`1000`, default `500`) — lighten/darken the
25
+ * resolved color (`<500` mixes white, `>500` mixes black).
26
+ * - `mixer` (`none` / `black` / `white`) &mdash; read-only output, auto-set
27
+ * whenever `shade` changes.
28
+ *
29
+ * **Adding custom color tokens.** If the predefined names are not enough,
30
+ * declare a new selector that supplies `--tp-element-color`:
31
+ *
32
+ * ```css
33
+ * tp-button[color="brand"] {
34
+ * --tp-element-color: hsl(265 75% 45%);
35
+ * }
36
+ * ```
37
+ *
38
+ * The shade math, hover/focus styles, and contrast color all derive from this
39
+ * one variable, so the new token "just works" with `shade`, `variant`, etc.
40
+ */
41
+ declare class ShadeMixerLitElement extends LitElement {
42
+ static rootStyles: any;
43
+ static shadedElementStyles: any;
44
+ color: PredefinedColors | string;
45
+ shade: number;
46
+ mixer: ShadeMixerType;
47
+ protected willUpdate(changed: Map<string, unknown>): void;
48
+ }
49
+
50
+ /**
51
+ * Visual treatment applied by `<tp-button>`.
52
+ *
53
+ * - `solid`: filled background using the resolved color.
54
+ * - `outline`: transparent background with a colored border and text.
55
+ * - `text`: no background or border &mdash; just colored text with an underline on hover.
56
+ */
57
+ type ButtonVariant = "solid" | "outline" | "text";
58
+ /**
59
+ * Mirrors the native HTML `<button>` `type` attribute.
60
+ *
61
+ * - `button`: no implicit form behaviour (default).
62
+ * - `submit`: clicks/Enter call `form.requestSubmit()` on the associated form.
63
+ * - `reset`: clicks/Enter call `form.reset()` on the associated form.
64
+ */
65
+ type ButtonType = "button" | "submit" | "reset";
66
+ declare global {
67
+ interface HTMLElementTagNameMap {
68
+ "tp-button": ButtonElement;
69
+ }
70
+ }
71
+ /**
72
+ * `<tp-button>` &mdash; framework-agnostic, form-associated button.
73
+ *
74
+ * Behaves like the native `<button>` element:
75
+ *
76
+ * - Participates in the tab order (`tabindex="0"` on connect; `-1` while disabled).
77
+ * - Activates on `Space` / `Enter`.
78
+ * - With `type="submit"` calls `form.requestSubmit()`; with `type="reset"` calls `form.reset()`.
79
+ * - When `disabled`, blocks click and keyboard activation and updates `aria-disabled`.
80
+ * - Announces `role="button"` to assistive technology via `ElementInternals`.
81
+ *
82
+ * Color is supplied through {@link ShadeMixerLitElement}: pick a palette token via `color`,
83
+ * then lighten or darken it through `shade` (`0`&ndash;`1000`, `500` is the base color).
84
+ * The element renders into the **light DOM** so global CSS, Tailwind utilities, and consumer
85
+ * tokens apply directly.
86
+ *
87
+ * @element tp-button
88
+ *
89
+ * @attr {"solid" | "outline" | "text"} [variant="solid"] - Visual treatment.
90
+ * @attr {"button" | "submit" | "reset"} [type="button"] - Native button semantics.
91
+ * @attr {boolean} [disabled=false] - Disables interaction and removes the button from the tab order.
92
+ * @attr {string} [color="contrast"] - Palette token (`primary`, `secondary`, `success`, &hellip;) inherited from {@link ShadeMixerLitElement}.
93
+ * @attr {number} [shade=500] - Shade between `0` (lightest) and `1000` (darkest); `500` is the base color.
94
+ * @attr {"none" | "black" | "white"} [mixer="none"] - Auto-derived from `shade` (read-only output).
95
+ *
96
+ * @cssprop --tp-button-color - Resolved background/text color (defaults to the calculated palette color).
97
+ * @cssprop --tp-button-contrast-color - Resolved contrast color used for `solid` text.
98
+ *
99
+ * @example
100
+ * ```html
101
+ * <tp-button>Click me</tp-button>
102
+ * <tp-button variant="outline" color="success">Save</tp-button>
103
+ * <tp-button variant="text" color="danger" disabled>Delete</tp-button>
104
+ *
105
+ * <form>
106
+ * <input name="email" type="email" />
107
+ * <tp-button type="submit" color="primary">Subscribe</tp-button>
108
+ * </form>
109
+ * ```
110
+ */
111
+ declare class ButtonElement extends ShadeMixerLitElement {
112
+ #private;
113
+ /** Marks the element as form-associated so `attachInternals().form` is populated. */
114
+ static formAssociated: boolean;
115
+ static styles: any;
116
+ variant: ButtonVariant;
117
+ /**
118
+ * Native button semantics. Determines what happens to the associated form when
119
+ * the button is activated. `submit` calls `form.requestSubmit()`, `reset` calls
120
+ * `form.reset()`, and `button` (the default) does nothing implicit.
121
+ */
122
+ type: ButtonType;
123
+ /**
124
+ * When `true`, the button is removed from the tab order, click and keyboard
125
+ * activation are blocked, and `aria-disabled` is set on the element internals.
126
+ */
127
+ disabled: boolean;
128
+ constructor();
129
+ /** The form this button is associated with, if any. Mirrors `HTMLButtonElement.form`. */
130
+ get form(): HTMLFormElement | null;
131
+ connectedCallback(): void;
132
+ disconnectedCallback(): void;
133
+ protected updated(changed: Map<string, unknown>): void;
134
+ protected createRenderRoot(): HTMLElement | DocumentFragment;
135
+ protected render(): any;
136
+ }
137
+
138
+ export { ButtonElement };
139
+ export type { ButtonType, ButtonVariant };
@@ -0,0 +1,2 @@
1
+ export { ButtonElement } from './button.element.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/fesm/elements.mjs CHANGED
@@ -11,6 +11,8 @@ import { uniqueId } from '@telperion/js-utils/unique-id';
11
11
  import { createContext, consume, provide } from '@lit/context';
12
12
  import { sliceBefore } from '@telperion/js-utils/array/slice-before';
13
13
  import { DomClone } from '@telperion/dom-utils';
14
+ import { uniquify } from '@telperion/js-utils/array/uniquify';
15
+ import { minMax } from '@telperion/js-utils/math/min-max';
14
16
 
15
17
  var _a$3, _b;
16
18
  /**
@@ -3295,5 +3297,327 @@ SelectElement = SelectElement_1 = __decorate([
3295
3297
  __metadata("design:paramtypes", [])
3296
3298
  ], SelectElement);
3297
3299
 
3298
- export { DetailsSetElement, DragElement, DragHandleElement, DropZoneElement, OptionElement, PopoverElement, SelectChangeEvent, SelectElement, SelectedContentElement, ThaDragEvent, ThaRoute, ThaRouter, ThaRouterConfig, ThaRouterLink, ThaRouterOutlet, ThaRouterParam };
3300
+ /**
3301
+ * Base class that powers Telperion's shared color/design system.
3302
+ *
3303
+ * Elements that extend `ShadeMixerLitElement` participate in a fully themable
3304
+ * palette driven entirely by CSS custom properties. The class:
3305
+ *
3306
+ * 1. Auto-injects a default palette onto `:root` &mdash; `--tp-color-white`,
3307
+ * `--tp-color-black`, `--tp-color-contrast`, `--tp-color-primary`,
3308
+ * `--tp-color-secondary`, `--tp-color-tertiary`, `--tp-color-quaternary`,
3309
+ * `--tp-color-success`, `--tp-color-danger`, `--tp-color-warning`, and
3310
+ * `--tp-color-neutral`. Override any of them at `:root` or any ancestor to
3311
+ * re-theme every shade-mixer-aware element underneath.
3312
+ * 2. Exposes `shadedElementStyles` &mdash; a `css` block that resolves the
3313
+ * selected `color` to `--tp-element-color`, applies the `shade`/`mixer`
3314
+ * math, and exposes the result as `--tp-calc-element-color` and
3315
+ * `--tp-calc-contrast-color` for consumers (variants, hover, focus, …).
3316
+ * Subclasses include it once in their own host rule.
3317
+ * 3. Provides three reflected attributes that drive the math:
3318
+ * - `color` &mdash; palette token name (predefined or custom).
3319
+ * - `shade` (`0`&ndash;`1000`, default `500`) &mdash; lighten/darken the
3320
+ * resolved color (`<500` mixes white, `>500` mixes black).
3321
+ * - `mixer` (`none` / `black` / `white`) &mdash; read-only output, auto-set
3322
+ * whenever `shade` changes.
3323
+ *
3324
+ * **Adding custom color tokens.** If the predefined names are not enough,
3325
+ * declare a new selector that supplies `--tp-element-color`:
3326
+ *
3327
+ * ```css
3328
+ * tp-button[color="brand"] {
3329
+ * --tp-element-color: hsl(265 75% 45%);
3330
+ * }
3331
+ * ```
3332
+ *
3333
+ * The shade math, hover/focus styles, and contrast color all derive from this
3334
+ * one variable, so the new token "just works" with `shade`, `variant`, etc.
3335
+ */
3336
+ class ShadeMixerLitElement extends LitElement {
3337
+ constructor() {
3338
+ super(...arguments);
3339
+ this.color = "contrast";
3340
+ this.shade = 500;
3341
+ this.mixer = "none";
3342
+ }
3343
+ static { this.rootStyles = (() => {
3344
+ const style = css `
3345
+ :root {
3346
+ --tp-color-white: hsl(0 0% 100%);
3347
+ --tp-color-black: hsl(0 0% 0%);
3348
+ --tp-color-contrast: var(--tp-color-black);
3349
+ --tp-color-primary: hsl(220 90% 56%);
3350
+ --tp-color-secondary: hsl(340 82% 52%);
3351
+ --tp-color-tertiary: hsl(50 100% 50%);
3352
+ --tp-color-quaternary: hsl(120 100% 40%);
3353
+ --tp-color-success: hsl(140 70% 40%);
3354
+ --tp-color-danger: hsl(0 70% 50%);
3355
+ --tp-color-warning: hsl(45 100% 50%);
3356
+ --tp-color-neutral: color-mix(in xyz, var(--tp-color-white) 50%, var(--tp-color-black) 50%);
3357
+ }
3358
+ `;
3359
+ document.adoptedStyleSheets = uniquify([...document.adoptedStyleSheets, style.styleSheet]);
3360
+ return style;
3361
+ })(); }
3362
+ static { this.shadedElementStyles = css `
3363
+ --tp-element-color: var(--tp-color-primary);
3364
+ --tp-shade-attr: attr(shade type(<number>));
3365
+ --tp-shade-mixer-type: attr(mixer type(*));
3366
+ --tp-shade-mixer-color: if(
3367
+ style(--tp-shade-mixer-type: black): var(--tp-color-black);
3368
+ else: var(--tp-color-white)
3369
+ );
3370
+ --tp-shade-mixer-percent: if(
3371
+ style(--tp-shade-mixer-type: black): calc(100% - ((var(--tp-shade-attr) - 500) / 500 * 100%));
3372
+ else: calc(100% - ((500 - var(--tp-shade-attr)) / 500 * 100%))
3373
+ );
3374
+ --tp-calc-element-color: color-mix(in xyz, var(--tp-element-color) var(--tp-shade-mixer-percent), var(--tp-shade-mixer-color) calc(100% - var(--tp-shade-mixer-percent)));
3375
+ --tp-calc-contrast-color: contrast-color(var(--tp-calc-element-color));
3376
+ --tp-contrast-color: if(
3377
+ style(--tp-calc-contrast-color: white): var(--tp-color-white);
3378
+ else: var(--tp-color-black);
3379
+ );
3380
+
3381
+ &[color="contrast"] { --tp-element-color: var(--tp-color-contrast); }
3382
+ &[color="primary"] { --tp-element-color: var(--tp-color-primary); }
3383
+ &[color="secondary"] { --tp-element-color: var(--tp-color-secondary); }
3384
+ &[color="tertiary"] { --tp-element-color: var(--tp-color-tertiary); }
3385
+ &[color="quaternary"] { --tp-element-color: var(--tp-color-quaternary); }
3386
+ &[color="success"] { --tp-element-color: var(--tp-color-success); }
3387
+ &[color="danger"] { --tp-element-color: var(--tp-color-danger); }
3388
+ &[color="warning"] { --tp-element-color: var(--tp-color-warning); }
3389
+ &[color="neutral"] { --tp-element-color: var(--tp-color-neutral); }
3390
+ `; }
3391
+ willUpdate(changed) {
3392
+ if (changed.has("shade")) {
3393
+ this.mixer = this.shade === 500 ? "none" : this.shade > 500 ? "black" : "white";
3394
+ }
3395
+ }
3396
+ }
3397
+ __decorate([
3398
+ property({ type: String, reflect: true }),
3399
+ __metadata("design:type", String)
3400
+ ], ShadeMixerLitElement.prototype, "color", void 0);
3401
+ __decorate([
3402
+ property({ type: Number, reflect: true, converter(value, type) {
3403
+ const num = Number(value);
3404
+ return isNaN(num) ? 500 : minMax(0, 1000, num);
3405
+ } }),
3406
+ __metadata("design:type", Number)
3407
+ ], ShadeMixerLitElement.prototype, "shade", void 0);
3408
+ __decorate([
3409
+ property({ type: String, reflect: true }),
3410
+ __metadata("design:type", String)
3411
+ ], ShadeMixerLitElement.prototype, "mixer", void 0);
3412
+
3413
+ /**
3414
+ * `<tp-button>` &mdash; framework-agnostic, form-associated button.
3415
+ *
3416
+ * Behaves like the native `<button>` element:
3417
+ *
3418
+ * - Participates in the tab order (`tabindex="0"` on connect; `-1` while disabled).
3419
+ * - Activates on `Space` / `Enter`.
3420
+ * - With `type="submit"` calls `form.requestSubmit()`; with `type="reset"` calls `form.reset()`.
3421
+ * - When `disabled`, blocks click and keyboard activation and updates `aria-disabled`.
3422
+ * - Announces `role="button"` to assistive technology via `ElementInternals`.
3423
+ *
3424
+ * Color is supplied through {@link ShadeMixerLitElement}: pick a palette token via `color`,
3425
+ * then lighten or darken it through `shade` (`0`&ndash;`1000`, `500` is the base color).
3426
+ * The element renders into the **light DOM** so global CSS, Tailwind utilities, and consumer
3427
+ * tokens apply directly.
3428
+ *
3429
+ * @element tp-button
3430
+ *
3431
+ * @attr {"solid" | "outline" | "text"} [variant="solid"] - Visual treatment.
3432
+ * @attr {"button" | "submit" | "reset"} [type="button"] - Native button semantics.
3433
+ * @attr {boolean} [disabled=false] - Disables interaction and removes the button from the tab order.
3434
+ * @attr {string} [color="contrast"] - Palette token (`primary`, `secondary`, `success`, &hellip;) inherited from {@link ShadeMixerLitElement}.
3435
+ * @attr {number} [shade=500] - Shade between `0` (lightest) and `1000` (darkest); `500` is the base color.
3436
+ * @attr {"none" | "black" | "white"} [mixer="none"] - Auto-derived from `shade` (read-only output).
3437
+ *
3438
+ * @cssprop --tp-button-color - Resolved background/text color (defaults to the calculated palette color).
3439
+ * @cssprop --tp-button-contrast-color - Resolved contrast color used for `solid` text.
3440
+ *
3441
+ * @example
3442
+ * ```html
3443
+ * <tp-button>Click me</tp-button>
3444
+ * <tp-button variant="outline" color="success">Save</tp-button>
3445
+ * <tp-button variant="text" color="danger" disabled>Delete</tp-button>
3446
+ *
3447
+ * <form>
3448
+ * <input name="email" type="email" />
3449
+ * <tp-button type="submit" color="primary">Subscribe</tp-button>
3450
+ * </form>
3451
+ * ```
3452
+ */
3453
+ let ButtonElement = class ButtonElement extends ShadeMixerLitElement {
3454
+ /** Marks the element as form-associated so `attachInternals().form` is populated. */
3455
+ static { this.formAssociated = true; }
3456
+ static { this.styles = (() => {
3457
+ const style = css `
3458
+ tp-button {
3459
+ ${ShadeMixerLitElement.shadedElementStyles}
3460
+ --tp-button-color: var(--tp-calc-element-color);
3461
+ --tp-button-contrast-color: var(--tp-calc-contrast-color);
3462
+ display: inline-block;
3463
+ border: 1px solid transparent;
3464
+ cursor: pointer;
3465
+ outline-offset: 1em;
3466
+ outline: 1px solid transparent;
3467
+ transform: scale(1);
3468
+ transition: all 0.18s ease;
3469
+ transition-property: background-color, color, outline-color, outline-offset, transform, border-width;
3470
+
3471
+ &:active {
3472
+ transform: scale(0.98);
3473
+ }
3474
+
3475
+ &:not(:active):focus {
3476
+ outline-color: var(--tp-button-color);
3477
+ outline-offset: 2px;
3478
+ }
3479
+
3480
+ &[disabled] {
3481
+ --tp-button-color: color-mix(in xyz, var(--tp-calc-element-color) 50%, var(--tp-color-white) 50%);
3482
+ --tp-button-contrast-color: rgb(from var(--tp-calc-contrast-color) r g b / 50%);
3483
+ pointer-events: none;
3484
+ user-select: none;
3485
+ cursor: not-allowed;
3486
+ }
3487
+
3488
+ &[variant="solid"],
3489
+ &[variant="outline"] {
3490
+ padding: 0.5em 1em;
3491
+ }
3492
+
3493
+ &[variant="solid"] {
3494
+ background-color: var(--tp-button-color);
3495
+ color: var(--tp-button-contrast-color);
3496
+
3497
+ &:hover {
3498
+ background-color: color-mix(in xyz, var(--tp-button-color) 80%, var(--tp-color-black) 20%);
3499
+ }
3500
+ }
3501
+
3502
+ &[variant="outline"] {
3503
+ color: var(--tp-button-color);
3504
+ border-color: var(--tp-button-color);
3505
+ background-color: transparent;
3506
+
3507
+ &:hover {
3508
+ background-color: color-mix(in xyz, var(--tp-button-color) 10%, transparent 90%);
3509
+ }
3510
+
3511
+ &:focus-visible {
3512
+ outline-color: transparent;
3513
+ }
3514
+ }
3515
+
3516
+ &[variant="text"] {
3517
+ color: var(--tp-button-color);
3518
+ background-color: transparent;
3519
+
3520
+ &:hover {
3521
+ color: color-mix(in xyz, var(--tp-button-color) 80%, var(--tp-color-black) 20%);
3522
+ text-decoration: underline;
3523
+ }
3524
+ }
3525
+ }
3526
+ `;
3527
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, style.styleSheet];
3528
+ return style;
3529
+ })(); }
3530
+ #internals;
3531
+ constructor() {
3532
+ super();
3533
+ this.variant = "solid";
3534
+ /**
3535
+ * Native button semantics. Determines what happens to the associated form when
3536
+ * the button is activated. `submit` calls `form.requestSubmit()`, `reset` calls
3537
+ * `form.reset()`, and `button` (the default) does nothing implicit.
3538
+ */
3539
+ this.type = "button";
3540
+ /**
3541
+ * When `true`, the button is removed from the tab order, click and keyboard
3542
+ * activation are blocked, and `aria-disabled` is set on the element internals.
3543
+ */
3544
+ this.disabled = false;
3545
+ this.#handleClick = (event) => {
3546
+ if (this.disabled) {
3547
+ event.preventDefault();
3548
+ event.stopImmediatePropagation();
3549
+ return;
3550
+ }
3551
+ const form = this.#internals.form;
3552
+ if (!form)
3553
+ return;
3554
+ if (this.type === "submit") {
3555
+ form.requestSubmit();
3556
+ }
3557
+ else if (this.type === "reset") {
3558
+ form.reset();
3559
+ }
3560
+ };
3561
+ this.#handleKeydown = (event) => {
3562
+ if (this.disabled)
3563
+ return;
3564
+ if (event.key === " " || event.key === "Enter") {
3565
+ event.preventDefault();
3566
+ this.click();
3567
+ }
3568
+ };
3569
+ this.#internals = this.attachInternals();
3570
+ this.#internals.role = "button";
3571
+ }
3572
+ /** The form this button is associated with, if any. Mirrors `HTMLButtonElement.form`. */
3573
+ get form() {
3574
+ return this.#internals.form;
3575
+ }
3576
+ connectedCallback() {
3577
+ super.connectedCallback();
3578
+ if (!this.hasAttribute("tabindex")) {
3579
+ this.setAttribute("tabindex", this.disabled ? "-1" : "0");
3580
+ }
3581
+ this.addEventListener("click", this.#handleClick);
3582
+ this.addEventListener("keydown", this.#handleKeydown);
3583
+ }
3584
+ disconnectedCallback() {
3585
+ super.disconnectedCallback();
3586
+ this.removeEventListener("click", this.#handleClick);
3587
+ this.removeEventListener("keydown", this.#handleKeydown);
3588
+ }
3589
+ updated(changed) {
3590
+ super.updated?.(changed);
3591
+ if (changed.has("disabled")) {
3592
+ this.setAttribute("tabindex", this.disabled ? "-1" : "0");
3593
+ this.#internals.ariaDisabled = this.disabled ? "true" : "false";
3594
+ }
3595
+ }
3596
+ #handleClick;
3597
+ #handleKeydown;
3598
+ createRenderRoot() {
3599
+ return this;
3600
+ }
3601
+ render() {
3602
+ return html$1 ``;
3603
+ }
3604
+ };
3605
+ __decorate([
3606
+ property({ type: String, reflect: true }),
3607
+ __metadata("design:type", String)
3608
+ ], ButtonElement.prototype, "variant", void 0);
3609
+ __decorate([
3610
+ property({ type: String, reflect: true }),
3611
+ __metadata("design:type", String)
3612
+ ], ButtonElement.prototype, "type", void 0);
3613
+ __decorate([
3614
+ property({ type: Boolean, reflect: true }),
3615
+ __metadata("design:type", Object)
3616
+ ], ButtonElement.prototype, "disabled", void 0);
3617
+ ButtonElement = __decorate([
3618
+ customElement("tp-button"),
3619
+ __metadata("design:paramtypes", [])
3620
+ ], ButtonElement);
3621
+
3622
+ export { ButtonElement, DetailsSetElement, DragElement, DragHandleElement, DropZoneElement, OptionElement, PopoverElement, SelectChangeEvent, SelectElement, SelectedContentElement, ThaDragEvent, ThaRoute, ThaRouter, ThaRouterConfig, ThaRouterLink, ThaRouterOutlet, ThaRouterParam };
3299
3623
  //# sourceMappingURL=elements.mjs.map