@prismora/elements 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +21 -2
- package/components.json +15600 -0
- package/custom-elements.json +56320 -0
- package/dist/base.d.ts +42 -0
- package/dist/base.js +62 -0
- package/dist/catalog.d.ts +33 -0
- package/dist/catalog.js +803 -0
- package/dist/components/accordion.d.ts +31 -0
- package/dist/components/accordion.js +63 -0
- package/dist/components/alert.d.ts +27 -0
- package/dist/components/alert.js +40 -0
- package/dist/components/avatar-group.d.ts +23 -0
- package/dist/components/avatar-group.js +43 -0
- package/dist/components/avatar.d.ts +24 -0
- package/dist/components/avatar.js +43 -0
- package/dist/components/back-to-top.d.ts +32 -0
- package/dist/components/back-to-top.js +50 -0
- package/dist/components/badge.d.ts +17 -0
- package/dist/components/badge.js +20 -0
- package/dist/components/banner.d.ts +34 -0
- package/dist/components/banner.js +54 -0
- package/dist/components/breadcrumb.d.ts +42 -0
- package/dist/components/breadcrumb.js +63 -0
- package/dist/components/button-group.d.ts +14 -0
- package/dist/components/button-group.js +28 -0
- package/dist/components/button.d.ts +59 -0
- package/dist/components/button.js +162 -0
- package/dist/components/calendar.d.ts +48 -0
- package/dist/components/calendar.js +296 -0
- package/dist/components/card.d.ts +23 -0
- package/dist/components/card.js +44 -0
- package/dist/components/carousel.d.ts +41 -0
- package/dist/components/carousel.js +156 -0
- package/dist/components/cascader.d.ts +70 -0
- package/dist/components/cascader.js +220 -0
- package/dist/components/chart.d.ts +59 -0
- package/dist/components/chart.js +267 -0
- package/dist/components/chat.d.ts +98 -0
- package/dist/components/chat.js +134 -0
- package/dist/components/checkbox-group.d.ts +48 -0
- package/dist/components/checkbox-group.js +111 -0
- package/dist/components/checkbox.d.ts +23 -0
- package/dist/components/checkbox.js +70 -0
- package/dist/components/code-block.d.ts +46 -0
- package/dist/components/code-block.js +86 -0
- package/dist/components/color-picker.d.ts +46 -0
- package/dist/components/color-picker.js +292 -0
- package/dist/components/combobox.d.ts +89 -0
- package/dist/components/combobox.js +384 -0
- package/dist/components/command.d.ts +96 -0
- package/dist/components/command.js +289 -0
- package/dist/components/context-menu.d.ts +23 -0
- package/dist/components/context-menu.js +110 -0
- package/dist/components/copy-button.d.ts +33 -0
- package/dist/components/copy-button.js +62 -0
- package/dist/components/countdown.d.ts +36 -0
- package/dist/components/countdown.js +56 -0
- package/dist/components/data-table.d.ts +180 -0
- package/dist/components/data-table.js +1060 -0
- package/dist/components/date-picker.d.ts +47 -0
- package/dist/components/date-picker.js +267 -0
- package/dist/components/description-list.d.ts +38 -0
- package/dist/components/description-list.js +41 -0
- package/dist/components/dialog.d.ts +47 -0
- package/dist/components/dialog.js +189 -0
- package/dist/components/divider.d.ts +16 -0
- package/dist/components/divider.js +23 -0
- package/dist/components/drawer.d.ts +19 -0
- package/dist/components/drawer.js +27 -0
- package/dist/components/editor.d.ts +50 -0
- package/dist/components/editor.js +474 -0
- package/dist/components/empty-state.d.ts +24 -0
- package/dist/components/empty-state.js +27 -0
- package/dist/components/file-upload.d.ts +56 -0
- package/dist/components/file-upload.js +176 -0
- package/dist/components/form-control.d.ts +69 -0
- package/dist/components/form-control.js +134 -0
- package/dist/components/form.d.ts +38 -0
- package/dist/components/form.js +161 -0
- package/dist/components/format-number.d.ts +37 -0
- package/dist/components/format-number.js +86 -0
- package/dist/components/icon.d.ts +34 -0
- package/dist/components/icon.js +47 -0
- package/dist/components/image.d.ts +44 -0
- package/dist/components/image.js +53 -0
- package/dist/components/indicator.d.ts +41 -0
- package/dist/components/indicator.js +65 -0
- package/dist/components/inline-edit.d.ts +45 -0
- package/dist/components/inline-edit.js +129 -0
- package/dist/components/input.d.ts +55 -0
- package/dist/components/input.js +186 -0
- package/dist/components/kbd.d.ts +15 -0
- package/dist/components/kbd.js +18 -0
- package/dist/components/list.d.ts +68 -0
- package/dist/components/list.js +180 -0
- package/dist/components/menu.d.ts +22 -0
- package/dist/components/menu.js +182 -0
- package/dist/components/menubar.d.ts +20 -0
- package/dist/components/menubar.js +120 -0
- package/dist/components/meter.d.ts +38 -0
- package/dist/components/meter.js +48 -0
- package/dist/components/nav.d.ts +91 -0
- package/dist/components/nav.js +228 -0
- package/dist/components/number-input.d.ts +26 -0
- package/dist/components/number-input.js +70 -0
- package/dist/components/pagination.d.ts +44 -0
- package/dist/components/pagination.js +94 -0
- package/dist/components/pin-input.d.ts +25 -0
- package/dist/components/pin-input.js +128 -0
- package/dist/components/popover.d.ts +23 -0
- package/dist/components/popover.js +140 -0
- package/dist/components/progress.d.ts +26 -0
- package/dist/components/progress.js +66 -0
- package/dist/components/pull-refresh.d.ts +41 -0
- package/dist/components/pull-refresh.js +101 -0
- package/dist/components/qr-code.d.ts +33 -0
- package/dist/components/qr-code.js +54 -0
- package/dist/components/radio-group.d.ts +37 -0
- package/dist/components/radio-group.js +82 -0
- package/dist/components/range-slider.d.ts +46 -0
- package/dist/components/range-slider.js +95 -0
- package/dist/components/rating.d.ts +38 -0
- package/dist/components/rating.js +80 -0
- package/dist/components/relative-time.d.ts +31 -0
- package/dist/components/relative-time.js +51 -0
- package/dist/components/scheduler.d.ts +84 -0
- package/dist/components/scheduler.js +187 -0
- package/dist/components/segmented.d.ts +58 -0
- package/dist/components/segmented.js +129 -0
- package/dist/components/select.d.ts +62 -0
- package/dist/components/select.js +140 -0
- package/dist/components/share-button.d.ts +37 -0
- package/dist/components/share-button.js +60 -0
- package/dist/components/signature.d.ts +54 -0
- package/dist/components/signature.js +152 -0
- package/dist/components/skeleton.d.ts +21 -0
- package/dist/components/skeleton.js +26 -0
- package/dist/components/slider.d.ts +31 -0
- package/dist/components/slider.js +80 -0
- package/dist/components/sortable.d.ts +22 -0
- package/dist/components/sortable.js +196 -0
- package/dist/components/sparkline.d.ts +28 -0
- package/dist/components/sparkline.js +46 -0
- package/dist/components/spinner.d.ts +16 -0
- package/dist/components/spinner.js +22 -0
- package/dist/components/splitter.d.ts +28 -0
- package/dist/components/splitter.js +89 -0
- package/dist/components/stat.d.ts +28 -0
- package/dist/components/stat.js +33 -0
- package/dist/components/steps.d.ts +53 -0
- package/dist/components/steps.js +91 -0
- package/dist/components/switch.d.ts +12 -0
- package/dist/components/switch.js +18 -0
- package/dist/components/table.d.ts +58 -0
- package/dist/components/table.js +96 -0
- package/dist/components/tabs.d.ts +62 -0
- package/dist/components/tabs.js +310 -0
- package/dist/components/tag.d.ts +32 -0
- package/dist/components/tag.js +58 -0
- package/dist/components/textarea.d.ts +25 -0
- package/dist/components/textarea.js +56 -0
- package/dist/components/theme-switch.d.ts +51 -0
- package/dist/components/theme-switch.js +85 -0
- package/dist/components/time-picker.d.ts +39 -0
- package/dist/components/time-picker.js +257 -0
- package/dist/components/timeline.d.ts +38 -0
- package/dist/components/timeline.js +56 -0
- package/dist/components/toaster.d.ts +25 -0
- package/dist/components/toaster.js +44 -0
- package/dist/components/toggle-group.d.ts +70 -0
- package/dist/components/toggle-group.js +172 -0
- package/dist/components/toolbar.d.ts +23 -0
- package/dist/components/toolbar.js +81 -0
- package/dist/components/tooltip.d.ts +18 -0
- package/dist/components/tooltip.js +92 -0
- package/dist/components/tour.d.ts +41 -0
- package/dist/components/tour.js +197 -0
- package/dist/components/transfer.d.ts +57 -0
- package/dist/components/transfer.js +161 -0
- package/dist/components/tree-select.d.ts +66 -0
- package/dist/components/tree-select.js +282 -0
- package/dist/components/tree.d.ts +129 -0
- package/dist/components/tree.js +427 -0
- package/dist/components/virtual-list.d.ts +41 -0
- package/dist/components/virtual-list.js +77 -0
- package/dist/confirm.d.ts +21 -0
- package/dist/confirm.js +69 -0
- package/dist/count.d.ts +5 -0
- package/dist/count.js +12 -0
- package/dist/data-table/icons.d.ts +8 -0
- package/dist/data-table/icons.js +10 -0
- package/dist/data-table/logic.d.ts +33 -0
- package/dist/data-table/logic.js +141 -0
- package/dist/data-table/strings.d.ts +37 -0
- package/dist/data-table/strings.js +36 -0
- package/dist/data-table/types.d.ts +76 -0
- package/dist/data-table/types.js +1 -0
- package/dist/define.d.ts +2 -0
- package/dist/define.js +2 -0
- package/dist/field-validity.d.ts +42 -0
- package/dist/field-validity.js +58 -0
- package/dist/floating.d.ts +13 -0
- package/dist/floating.js +65 -0
- package/dist/highlight.d.ts +11 -0
- package/dist/highlight.js +103 -0
- package/dist/id.d.ts +2 -0
- package/dist/id.js +3 -0
- package/dist/index.d.ts +102 -0
- package/dist/index.js +102 -0
- package/dist/manifest.d.ts +51 -0
- package/dist/manifest.js +11 -0
- package/dist/named-icon.d.ts +8 -0
- package/dist/named-icon.js +8 -0
- package/dist/patterns/account.d.ts +6 -0
- package/dist/patterns/account.js +137 -0
- package/dist/patterns/app.d.ts +7 -0
- package/dist/patterns/app.js +351 -0
- package/dist/patterns/commerce.d.ts +4 -0
- package/dist/patterns/commerce.js +200 -0
- package/dist/patterns/flows.d.ts +3 -0
- package/dist/patterns/flows.js +100 -0
- package/dist/patterns/index.d.ts +9 -0
- package/dist/patterns/index.js +29 -0
- package/dist/patterns/marketing.d.ts +4 -0
- package/dist/patterns/marketing.js +173 -0
- package/dist/patterns/more.d.ts +15 -0
- package/dist/patterns/more.js +581 -0
- package/dist/patterns/shell.d.ts +9 -0
- package/dist/patterns/shell.js +48 -0
- package/dist/patterns/types.d.ts +19 -0
- package/dist/patterns/types.js +1 -0
- package/dist/position.d.ts +2 -0
- package/dist/position.js +33 -0
- package/dist/prismora.standalone.js +5222 -0
- package/dist/prismora.standalone.js.map +199 -0
- package/dist/qr.d.ts +14 -0
- package/dist/qr.js +258 -0
- package/dist/register.d.ts +8 -0
- package/dist/register.js +21 -0
- package/dist/registry.d.ts +203 -0
- package/dist/registry.js +203 -0
- package/dist/remote.d.ts +58 -0
- package/dist/remote.js +130 -0
- package/dist/sheet.d.ts +34 -0
- package/dist/sheet.js +153 -0
- package/dist/styles/accordion.d.ts +2 -0
- package/dist/styles/accordion.js +3 -0
- package/dist/styles/alert.d.ts +2 -0
- package/dist/styles/alert.js +3 -0
- package/dist/styles/avatar.d.ts +2 -0
- package/dist/styles/avatar.js +3 -0
- package/dist/styles/badge.d.ts +2 -0
- package/dist/styles/badge.js +3 -0
- package/dist/styles/banner.d.ts +2 -0
- package/dist/styles/banner.js +3 -0
- package/dist/styles/breadcrumb.d.ts +2 -0
- package/dist/styles/breadcrumb.js +3 -0
- package/dist/styles/button.d.ts +2 -0
- package/dist/styles/button.js +3 -0
- package/dist/styles/calendar.d.ts +2 -0
- package/dist/styles/calendar.js +3 -0
- package/dist/styles/card.d.ts +2 -0
- package/dist/styles/card.js +3 -0
- package/dist/styles/carousel.d.ts +2 -0
- package/dist/styles/carousel.js +3 -0
- package/dist/styles/chart.d.ts +2 -0
- package/dist/styles/chart.js +3 -0
- package/dist/styles/chat.d.ts +2 -0
- package/dist/styles/chat.js +3 -0
- package/dist/styles/checkbox.d.ts +2 -0
- package/dist/styles/checkbox.js +3 -0
- package/dist/styles/color-picker.d.ts +2 -0
- package/dist/styles/color-picker.js +3 -0
- package/dist/styles/combobox.d.ts +2 -0
- package/dist/styles/combobox.js +3 -0
- package/dist/styles/command.d.ts +2 -0
- package/dist/styles/command.js +3 -0
- package/dist/styles/data-table.d.ts +2 -0
- package/dist/styles/data-table.js +3 -0
- package/dist/styles/description-list.d.ts +2 -0
- package/dist/styles/description-list.js +3 -0
- package/dist/styles/dialog.d.ts +2 -0
- package/dist/styles/dialog.js +3 -0
- package/dist/styles/drawer.d.ts +2 -0
- package/dist/styles/drawer.js +3 -0
- package/dist/styles/editor.d.ts +2 -0
- package/dist/styles/editor.js +3 -0
- package/dist/styles/empty-state.d.ts +2 -0
- package/dist/styles/empty-state.js +3 -0
- package/dist/styles/extras.d.ts +2 -0
- package/dist/styles/extras.js +3 -0
- package/dist/styles/field.d.ts +2 -0
- package/dist/styles/field.js +3 -0
- package/dist/styles/file-upload.d.ts +2 -0
- package/dist/styles/file-upload.js +3 -0
- package/dist/styles/foundation.d.ts +2 -0
- package/dist/styles/foundation.js +3 -0
- package/dist/styles/helpers.d.ts +2 -0
- package/dist/styles/helpers.js +3 -0
- package/dist/styles/icon.d.ts +2 -0
- package/dist/styles/icon.js +3 -0
- package/dist/styles/image.d.ts +2 -0
- package/dist/styles/image.js +3 -0
- package/dist/styles/indicator.d.ts +2 -0
- package/dist/styles/indicator.js +3 -0
- package/dist/styles/input.d.ts +2 -0
- package/dist/styles/input.js +3 -0
- package/dist/styles/kbd.d.ts +2 -0
- package/dist/styles/kbd.js +3 -0
- package/dist/styles/list.d.ts +2 -0
- package/dist/styles/list.js +3 -0
- package/dist/styles/listbox.d.ts +2 -0
- package/dist/styles/listbox.js +3 -0
- package/dist/styles/meter.d.ts +2 -0
- package/dist/styles/meter.js +3 -0
- package/dist/styles/nav.d.ts +2 -0
- package/dist/styles/nav.js +3 -0
- package/dist/styles/number-input.d.ts +2 -0
- package/dist/styles/number-input.js +3 -0
- package/dist/styles/overlay.d.ts +2 -0
- package/dist/styles/overlay.js +3 -0
- package/dist/styles/pagination.d.ts +2 -0
- package/dist/styles/pagination.js +3 -0
- package/dist/styles/pickers.d.ts +2 -0
- package/dist/styles/pickers.js +3 -0
- package/dist/styles/pin-input.d.ts +2 -0
- package/dist/styles/pin-input.js +3 -0
- package/dist/styles/popover.d.ts +2 -0
- package/dist/styles/popover.js +3 -0
- package/dist/styles/progress.d.ts +2 -0
- package/dist/styles/progress.js +3 -0
- package/dist/styles/pull-refresh.d.ts +2 -0
- package/dist/styles/pull-refresh.js +3 -0
- package/dist/styles/range-slider.d.ts +2 -0
- package/dist/styles/range-slider.js +3 -0
- package/dist/styles/rating.d.ts +2 -0
- package/dist/styles/rating.js +3 -0
- package/dist/styles/scheduler.d.ts +2 -0
- package/dist/styles/scheduler.js +3 -0
- package/dist/styles/segmented.d.ts +2 -0
- package/dist/styles/segmented.js +3 -0
- package/dist/styles/select.d.ts +2 -0
- package/dist/styles/select.js +3 -0
- package/dist/styles/separator.d.ts +2 -0
- package/dist/styles/separator.js +3 -0
- package/dist/styles/sheet.d.ts +2 -0
- package/dist/styles/sheet.js +3 -0
- package/dist/styles/skeleton.d.ts +2 -0
- package/dist/styles/skeleton.js +3 -0
- package/dist/styles/slider.d.ts +2 -0
- package/dist/styles/slider.js +3 -0
- package/dist/styles/sortable.d.ts +2 -0
- package/dist/styles/sortable.js +3 -0
- package/dist/styles/spinner.d.ts +2 -0
- package/dist/styles/spinner.js +3 -0
- package/dist/styles/splitter.d.ts +2 -0
- package/dist/styles/splitter.js +3 -0
- package/dist/styles/stat.d.ts +2 -0
- package/dist/styles/stat.js +3 -0
- package/dist/styles/steps.d.ts +2 -0
- package/dist/styles/steps.js +3 -0
- package/dist/styles/surface.d.ts +2 -0
- package/dist/styles/surface.js +3 -0
- package/dist/styles/switch.d.ts +2 -0
- package/dist/styles/switch.js +3 -0
- package/dist/styles/table.d.ts +2 -0
- package/dist/styles/table.js +3 -0
- package/dist/styles/tabs.d.ts +2 -0
- package/dist/styles/tabs.js +3 -0
- package/dist/styles/tag.d.ts +2 -0
- package/dist/styles/tag.js +3 -0
- package/dist/styles/time-picker.d.ts +2 -0
- package/dist/styles/time-picker.js +3 -0
- package/dist/styles/timeline.d.ts +2 -0
- package/dist/styles/timeline.js +3 -0
- package/dist/styles/toast.d.ts +2 -0
- package/dist/styles/toast.js +3 -0
- package/dist/styles/toggle.d.ts +2 -0
- package/dist/styles/toggle.js +3 -0
- package/dist/styles/toolbar.d.ts +2 -0
- package/dist/styles/toolbar.js +3 -0
- package/dist/styles/tooltip.d.ts +2 -0
- package/dist/styles/tooltip.js +3 -0
- package/dist/styles/tree.d.ts +2 -0
- package/dist/styles/tree.js +3 -0
- package/dist/table-core.d.ts +75 -0
- package/dist/table-core.js +205 -0
- package/dist/toast.d.ts +42 -0
- package/dist/toast.js +206 -0
- package/dist/top-layer.d.ts +4 -0
- package/dist/top-layer.js +33 -0
- package/package.json +101 -4
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { PmElement, defineElement } from '../base.js';
|
|
3
|
+
import { FieldValidity } from '../field-validity.js';
|
|
4
|
+
import fieldCss from '../styles/field.js';
|
|
5
|
+
import segmentedCss from '../styles/segmented.js';
|
|
6
|
+
/**
|
|
7
|
+
* Segmented control: pick one of a few options (view switchers, filters). Radio-group semantics, form-associated.
|
|
8
|
+
* @tag pm-segmented
|
|
9
|
+
* @slot - `pm-segment` elements.
|
|
10
|
+
* @fires pm-change - Selection changed. detail: { value }
|
|
11
|
+
* @example <pm-segmented label="View" value="grid"><pm-segment value="list">List</pm-segment><pm-segment value="grid">Grid</pm-segment><pm-segment value="board">Board</pm-segment></pm-segmented>
|
|
12
|
+
*/
|
|
13
|
+
export class PmSegmented extends PmElement {
|
|
14
|
+
static { this.componentName = 'segmented'; }
|
|
15
|
+
static { this.formAssociated = true; }
|
|
16
|
+
static { this.styles = [...PmElement.styles, fieldCss, segmentedCss, css `:host { display: inline-flex; } :host([full]) { display: flex; } :host([full]) .pm-segmented ::slotted(*) { flex: 1; } .pm-field { justify-items: start; } :host([full]) .pm-field { justify-items: stretch; flex: 1; }`]; }
|
|
17
|
+
static { this.properties = { hint: {}, error: {}, required: { type: Boolean, reflect: true },
|
|
18
|
+
value: { reflect: true }, name: { reflect: true }, label: {}, size: { reflect: true }, full: { type: Boolean, reflect: true }, disabled: { type: Boolean, reflect: true },
|
|
19
|
+
}; }
|
|
20
|
+
#internals = this.attachInternals();
|
|
21
|
+
#field = new FieldValidity(this, this.#internals, {
|
|
22
|
+
hint: () => this.hint, error: () => this.error, missing: () => this.required && !this.value && !this.disabled,
|
|
23
|
+
missingMessage: 'Please choose an option.', anchor: () => this.#segments().find((s) => s.tabIndex === 0),
|
|
24
|
+
});
|
|
25
|
+
#default;
|
|
26
|
+
constructor() { super(); this.full = false; this.disabled = false; this.required = false; }
|
|
27
|
+
connectedCallback() {
|
|
28
|
+
super.connectedCallback();
|
|
29
|
+
this.#default = this.getAttribute('value') ?? undefined;
|
|
30
|
+
this.addEventListener('keydown', this.#onKey);
|
|
31
|
+
this.addEventListener('click', this.#onClick);
|
|
32
|
+
}
|
|
33
|
+
formResetCallback() { this.value = this.#default; this.#field.reset(); }
|
|
34
|
+
formStateRestoreCallback(v) { this.value = v; }
|
|
35
|
+
formDisabledCallback(d) { this.disabled = d; }
|
|
36
|
+
#segments() { return [...this.children].filter((e) => e instanceof PmSegment); }
|
|
37
|
+
render() {
|
|
38
|
+
const group = html `<div class="pm-segmented" part="base" role="radiogroup" aria-label=${this.label ?? nothing} aria-disabled=${this.disabled ? 'true' : nothing}
|
|
39
|
+
aria-required=${this.required ? 'true' : nothing} aria-invalid=${this.#field.invalid ? 'true' : nothing} aria-describedby=${this.#field.describedBy ?? nothing}
|
|
40
|
+
data-size=${this.size ?? nothing} ?data-full=${this.full}><slot @slotchange=${this.#sync}></slot></div>`;
|
|
41
|
+
return this.hint || this.#field.message ? html `<div class="pm-field" part="field">${group}${this.#field.messages()}</div>` : group;
|
|
42
|
+
}
|
|
43
|
+
updated() {
|
|
44
|
+
this.#sync();
|
|
45
|
+
this.#internals.setFormValue(this.value ?? null);
|
|
46
|
+
}
|
|
47
|
+
#sync = () => {
|
|
48
|
+
const segs = this.#segments();
|
|
49
|
+
if (this.value === undefined && segs.length)
|
|
50
|
+
this.value = segs.find((s) => !s.disabled)?.value;
|
|
51
|
+
const focusable = segs.find((s) => s.value === this.value && !s.disabled) ?? segs.find((s) => !s.disabled);
|
|
52
|
+
for (const s of segs) {
|
|
53
|
+
s.checked = s.value === this.value;
|
|
54
|
+
s.groupDisabled = this.disabled;
|
|
55
|
+
s.tabIndex = s === focusable && !this.disabled ? 0 : -1;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
#select(seg, focus = false) {
|
|
59
|
+
if (seg.disabled || this.disabled)
|
|
60
|
+
return;
|
|
61
|
+
if (focus)
|
|
62
|
+
seg.focus();
|
|
63
|
+
if (seg.value === this.value)
|
|
64
|
+
return;
|
|
65
|
+
this.value = seg.value;
|
|
66
|
+
this.#field.touch();
|
|
67
|
+
this.emit('pm-change', { value: this.value });
|
|
68
|
+
}
|
|
69
|
+
#onClick = (e) => {
|
|
70
|
+
const seg = e.target.closest?.('pm-segment');
|
|
71
|
+
if (seg instanceof PmSegment)
|
|
72
|
+
this.#select(seg);
|
|
73
|
+
};
|
|
74
|
+
#onKey = (e) => {
|
|
75
|
+
const segs = this.#segments().filter((s) => !s.disabled);
|
|
76
|
+
const i = segs.findIndex((s) => s.value === this.value);
|
|
77
|
+
const rtl = getComputedStyle(this).direction === 'rtl';
|
|
78
|
+
const step = { ArrowRight: rtl ? -1 : 1, ArrowDown: 1, ArrowLeft: rtl ? 1 : -1, ArrowUp: -1 }[e.key];
|
|
79
|
+
let next;
|
|
80
|
+
if (step)
|
|
81
|
+
next = segs[(i + step + segs.length) % segs.length];
|
|
82
|
+
else if (e.key === 'Home')
|
|
83
|
+
next = segs[0];
|
|
84
|
+
else if (e.key === 'End')
|
|
85
|
+
next = segs[segs.length - 1];
|
|
86
|
+
else if (e.key === ' ' || e.key === 'Enter') {
|
|
87
|
+
const t = e.target.closest?.('pm-segment');
|
|
88
|
+
if (t instanceof PmSegment) {
|
|
89
|
+
e.preventDefault();
|
|
90
|
+
this.#select(t);
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (!next)
|
|
95
|
+
return;
|
|
96
|
+
e.preventDefault();
|
|
97
|
+
this.#select(next, true);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* One option of a segmented control.
|
|
102
|
+
* @tag pm-segment
|
|
103
|
+
* @slot - Label (text and/or icon).
|
|
104
|
+
* @example <pm-segment value="grid">Grid</pm-segment>
|
|
105
|
+
*/
|
|
106
|
+
export class PmSegment extends PmElement {
|
|
107
|
+
static { this.componentName = 'segment'; }
|
|
108
|
+
static { this.styles = [...PmElement.styles, segmentedCss, css `:host { display: inline-flex; border-radius: var(--pm-radius-control, var(--pm-radius-md)); outline: none; } :host(:focus-visible) .pm-segment { outline: 2px solid var(--pm-color-ring); outline-offset: 1px; } .pm-segment { inline-size: 100%; }`]; }
|
|
109
|
+
static { this.properties = {
|
|
110
|
+
value: { reflect: true }, disabled: { type: Boolean, reflect: true }, label: {},
|
|
111
|
+
checked: { attribute: false }, groupDisabled: { attribute: false },
|
|
112
|
+
}; }
|
|
113
|
+
constructor() { super(); this.value = ''; this.disabled = false; this.checked = false; this.groupDisabled = false; }
|
|
114
|
+
connectedCallback() { super.connectedCallback(); this.setAttribute('role', 'radio'); }
|
|
115
|
+
updated() {
|
|
116
|
+
this.setAttribute('aria-checked', String(this.checked));
|
|
117
|
+
if (this.label)
|
|
118
|
+
this.setAttribute('aria-label', this.label);
|
|
119
|
+
if (this.disabled || this.groupDisabled)
|
|
120
|
+
this.setAttribute('aria-disabled', 'true');
|
|
121
|
+
else
|
|
122
|
+
this.removeAttribute('aria-disabled');
|
|
123
|
+
}
|
|
124
|
+
render() {
|
|
125
|
+
return html `<span class="pm-segment" part="base" ?data-checked=${this.checked} ?data-disabled=${this.disabled || this.groupDisabled}><slot></slot></span>`;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
defineElement('pm-segmented', PmSegmented);
|
|
129
|
+
defineElement('pm-segment', PmSegment);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type PropertyDeclarations, type PropertyValues } from 'lit';
|
|
2
|
+
import { PmFormControl } from './form-control.js';
|
|
3
|
+
export interface SelectOption {
|
|
4
|
+
value: string;
|
|
5
|
+
label?: string;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Native-backed select for picking one option from a short list, with the platform picker on phones.
|
|
10
|
+
*
|
|
11
|
+
* Options are light-DOM `<option>` / `<optgroup>` children (or the `options` property) mirrored into a native
|
|
12
|
+
* `<select>`, so it keeps the platform picker on phones and full keyboard/autofill support on desktop (customizable
|
|
13
|
+
* via `appearance: base-select` where supported). Set `placeholder` for an empty first choice. For search,
|
|
14
|
+
* multi-select or creating values use `<pm-combobox>`.
|
|
15
|
+
*
|
|
16
|
+
* Options from an API: `src="/api/countries"` with `options-path`, `value-key`, `label-key` and `group-key` (or the
|
|
17
|
+
* `loader` property and `mapOption`) loads them once on connect, showing a loading and an error state; `reload()`
|
|
18
|
+
* loads them again. A `value` set before they arrive is kept.
|
|
19
|
+
* @tag pm-select
|
|
20
|
+
* @fires pm-load - Options arrived from the server. detail: { options }
|
|
21
|
+
* @fires pm-load-error - Loading options failed. detail: { error }
|
|
22
|
+
* @example <pm-select label="Plan"><option value="pro">Pro</option><option value="team">Team</option></pm-select>
|
|
23
|
+
*/
|
|
24
|
+
export declare class PmSelect extends PmFormControl {
|
|
25
|
+
#private;
|
|
26
|
+
static styles: import("lit").CSSResult[];
|
|
27
|
+
static componentName: string;
|
|
28
|
+
static properties: PropertyDeclarations;
|
|
29
|
+
/** Empty first choice shown until a value is picked (required selects then fail validation until chosen). */
|
|
30
|
+
placeholder?: string;
|
|
31
|
+
/** Browser autofill hint, as on a native select (e.g. "country", "shipping country"). */
|
|
32
|
+
autocomplete?: string;
|
|
33
|
+
/** Choices as data: strings, `{ value, label, disabled }`, or `{ group, options: [...] }`. */
|
|
34
|
+
options?: (string | SelectOption | {
|
|
35
|
+
group: string;
|
|
36
|
+
options: (string | SelectOption)[];
|
|
37
|
+
})[];
|
|
38
|
+
/** URL of the options (JSON). */
|
|
39
|
+
src?: string;
|
|
40
|
+
/** Your own options loader (takes precedence over `src`): `async ({ signal }) => items`. */
|
|
41
|
+
loader?: (ctx: {
|
|
42
|
+
signal: AbortSignal;
|
|
43
|
+
}) => Promise<unknown>;
|
|
44
|
+
/** Turn one server item into an option. */
|
|
45
|
+
mapOption?: (item: unknown) => SelectOption & {
|
|
46
|
+
group?: string;
|
|
47
|
+
};
|
|
48
|
+
/** Where the list is in the response (dotted path; empty = the response itself). */
|
|
49
|
+
optionsPath?: string;
|
|
50
|
+
/** Item field for the value (default `value`, then `id`). */
|
|
51
|
+
valueKey?: string;
|
|
52
|
+
/** Item field for the label (default `label`, then `name`, then `title`). */
|
|
53
|
+
labelKey?: string;
|
|
54
|
+
/** Item field for the group (`<optgroup>`) label (default `group`). */
|
|
55
|
+
groupKey?: string;
|
|
56
|
+
/** Load the options from `src` / `loader` again. */
|
|
57
|
+
reload(): void;
|
|
58
|
+
connectedCallback(): void;
|
|
59
|
+
disconnectedCallback(): void;
|
|
60
|
+
protected willUpdate(changed: PropertyValues<this>): void;
|
|
61
|
+
render(): import("lit").TemplateResult<1>;
|
|
62
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { html, nothing } from 'lit';
|
|
2
|
+
import { ifDefined } from 'lit/directives/if-defined.js';
|
|
3
|
+
import { defineElement } from '../base.js';
|
|
4
|
+
import { RemoteRequest, fetchJson, field, pick } from '../remote.js';
|
|
5
|
+
import { PmFormControl } from './form-control.js';
|
|
6
|
+
import inputCss from '../styles/input.js';
|
|
7
|
+
import selectCss from '../styles/select.js';
|
|
8
|
+
/**
|
|
9
|
+
* Native-backed select for picking one option from a short list, with the platform picker on phones.
|
|
10
|
+
*
|
|
11
|
+
* Options are light-DOM `<option>` / `<optgroup>` children (or the `options` property) mirrored into a native
|
|
12
|
+
* `<select>`, so it keeps the platform picker on phones and full keyboard/autofill support on desktop (customizable
|
|
13
|
+
* via `appearance: base-select` where supported). Set `placeholder` for an empty first choice. For search,
|
|
14
|
+
* multi-select or creating values use `<pm-combobox>`.
|
|
15
|
+
*
|
|
16
|
+
* Options from an API: `src="/api/countries"` with `options-path`, `value-key`, `label-key` and `group-key` (or the
|
|
17
|
+
* `loader` property and `mapOption`) loads them once on connect, showing a loading and an error state; `reload()`
|
|
18
|
+
* loads them again. A `value` set before they arrive is kept.
|
|
19
|
+
* @tag pm-select
|
|
20
|
+
* @fires pm-load - Options arrived from the server. detail: { options }
|
|
21
|
+
* @fires pm-load-error - Loading options failed. detail: { error }
|
|
22
|
+
* @example <pm-select label="Plan"><option value="pro">Pro</option><option value="team">Team</option></pm-select>
|
|
23
|
+
*/
|
|
24
|
+
export class PmSelect extends PmFormControl {
|
|
25
|
+
static { this.styles = [...PmFormControl.styles, inputCss, selectCss]; }
|
|
26
|
+
static { this.componentName = 'select'; }
|
|
27
|
+
static { this.properties = {
|
|
28
|
+
placeholder: {}, autocomplete: {}, options: { type: Array }, src: {}, loader: { attribute: false }, mapOption: { attribute: false },
|
|
29
|
+
optionsPath: { attribute: 'options-path' }, valueKey: { attribute: 'value-key' }, labelKey: { attribute: 'label-key' }, groupKey: { attribute: 'group-key' },
|
|
30
|
+
}; }
|
|
31
|
+
#remote = new RemoteRequest(this, {
|
|
32
|
+
run: async (_q, ctx) => {
|
|
33
|
+
const data = this.loader ? await this.loader(ctx) : await fetchJson(this.src, ctx.signal);
|
|
34
|
+
const list = pick(data, this.optionsPath);
|
|
35
|
+
if (!Array.isArray(list))
|
|
36
|
+
throw new Error(`Expected a list${this.optionsPath ? ` at "${this.optionsPath}"` : ''} of options`);
|
|
37
|
+
return list.map((item) => this.mapOption?.(item) ?? this.#toOption(item));
|
|
38
|
+
},
|
|
39
|
+
done: (opts) => {
|
|
40
|
+
const groups = new Map();
|
|
41
|
+
for (const o of opts) {
|
|
42
|
+
if (!groups.has(o.group))
|
|
43
|
+
groups.set(o.group, []);
|
|
44
|
+
groups.get(o.group).push(o);
|
|
45
|
+
}
|
|
46
|
+
this.options = [...groups].flatMap(([g, list]) => (g ? [{ group: g, options: list }] : list));
|
|
47
|
+
this.emit('pm-load', { options: opts });
|
|
48
|
+
},
|
|
49
|
+
failed: (error) => this.emit('pm-load-error', { error }),
|
|
50
|
+
});
|
|
51
|
+
#toOption(item) {
|
|
52
|
+
if (item == null || typeof item !== 'object')
|
|
53
|
+
return { value: String(item), label: String(item) };
|
|
54
|
+
const value = String(field(item, [this.valueKey, 'value', 'id']) ?? '');
|
|
55
|
+
const group = field(item, [this.groupKey, 'group']);
|
|
56
|
+
return { value, label: String(field(item, [this.labelKey, 'label', 'name', 'title']) ?? value), ...(group != null ? { group: String(group) } : {}),
|
|
57
|
+
...(item.disabled ? { disabled: true } : {}) };
|
|
58
|
+
}
|
|
59
|
+
/** Load the options from `src` / `loader` again. */
|
|
60
|
+
reload() { if (this.src || this.loader)
|
|
61
|
+
this.#remote.request(null, true); }
|
|
62
|
+
#observer;
|
|
63
|
+
#options = [];
|
|
64
|
+
connectedCallback() {
|
|
65
|
+
super.connectedCallback();
|
|
66
|
+
this.#read();
|
|
67
|
+
this.#observer = new MutationObserver(() => { this.#read(); this.requestUpdate(); });
|
|
68
|
+
this.#observer.observe(this, { childList: true, subtree: true, characterData: true, attributes: true });
|
|
69
|
+
}
|
|
70
|
+
disconnectedCallback() {
|
|
71
|
+
super.disconnectedCallback();
|
|
72
|
+
this.#observer?.disconnect();
|
|
73
|
+
}
|
|
74
|
+
#read() {
|
|
75
|
+
const opts = [...this.querySelectorAll('option')];
|
|
76
|
+
const norm = (o, group) => (typeof o === 'string' ? { value: o, label: o, group } : { ...o, label: o.label ?? o.value, group });
|
|
77
|
+
this.#options = this.options?.length
|
|
78
|
+
? this.options.flatMap((o) => (typeof o === 'object' && 'group' in o ? o.options.map((x) => norm(x, o.group)) : [norm(o)]))
|
|
79
|
+
: opts.map((o) => ({ value: o.value, label: o.textContent?.trim() ?? o.value, disabled: o.disabled, group: o.parentElement?.closest('optgroup')?.label }));
|
|
80
|
+
if (!this.value && !this.placeholder && !((this.src || this.loader) && !this.options?.length)) {
|
|
81
|
+
const selected = opts.find((o) => o.selected && o.hasAttribute('selected'))?.value ?? this.#options[0]?.value;
|
|
82
|
+
if (selected !== undefined)
|
|
83
|
+
this.value = selected;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
willUpdate(changed) {
|
|
87
|
+
super.willUpdate(changed);
|
|
88
|
+
if ((changed.has('src') || changed.has('loader')) && (this.src || this.loader))
|
|
89
|
+
this.#remote.request(null, true);
|
|
90
|
+
if (changed.has('options')) {
|
|
91
|
+
if (typeof this.options === 'string') {
|
|
92
|
+
try {
|
|
93
|
+
this.options = JSON.parse(this.options);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
this.options = [];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
this.#read();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
#groups() {
|
|
103
|
+
const out = [];
|
|
104
|
+
for (const o of this.#options) {
|
|
105
|
+
const last = out[out.length - 1];
|
|
106
|
+
if (last && last[0] === o.group)
|
|
107
|
+
last[1].push(o);
|
|
108
|
+
else
|
|
109
|
+
out.push([o.group, [o]]);
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
render() {
|
|
114
|
+
return this.field(html `<select
|
|
115
|
+
data-control
|
|
116
|
+
part="control"
|
|
117
|
+
class="pm-select"
|
|
118
|
+
id=${this.controlId}
|
|
119
|
+
name=${ifDefined(this.name)}
|
|
120
|
+
autocomplete=${ifDefined(this.autocomplete)}
|
|
121
|
+
data-size=${ifDefined(this.size)}
|
|
122
|
+
?required=${this.required}
|
|
123
|
+
?disabled=${this.disabled}
|
|
124
|
+
aria-invalid=${this.showInvalid ? 'true' : nothing}
|
|
125
|
+
aria-describedby=${ifDefined(this.describedBy())}
|
|
126
|
+
aria-busy=${this.#remote.loading ? 'true' : nothing}
|
|
127
|
+
@input=${this.onInput}
|
|
128
|
+
@change=${this.onChange}
|
|
129
|
+
>
|
|
130
|
+
${this.#remote.loading && !this.#options.length ? html `<option value="" selected disabled>Loading…</option>`
|
|
131
|
+
: this.#remote.error && !this.#options.length ? html `<option value="" selected disabled>Couldn't load options</option>` : nothing}
|
|
132
|
+
${this.placeholder ? html `<option value="" ?selected=${!this.value} disabled hidden>${this.placeholder}</option>` : nothing}
|
|
133
|
+
${this.#groups().map(([g, list]) => {
|
|
134
|
+
const items = list.map((o) => html `<option value=${o.value} ?disabled=${o.disabled} ?selected=${o.value === this.value}>${o.label}</option>`);
|
|
135
|
+
return g ? html `<optgroup label=${g}>${items}</optgroup>` : items;
|
|
136
|
+
})}
|
|
137
|
+
</select>`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
defineElement('pm-select', PmSelect);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type PropertyDeclarations } from 'lit';
|
|
2
|
+
import { PmElement } from '../base.js';
|
|
3
|
+
/**
|
|
4
|
+
* Share button: opens the device's share sheet (phones, Safari, Edge) and copies the link where there is none.
|
|
5
|
+
*
|
|
6
|
+
* The page address is shared unless `url` is set. After a copy the button says so, visibly and to screen readers.
|
|
7
|
+
* @tag pm-share-button
|
|
8
|
+
* @fires pm-share - Shared or copied. detail: { url, method: 'share' | 'copy' }
|
|
9
|
+
* @fires pm-error - Sharing and copying both failed. detail: { error }
|
|
10
|
+
* @csspart base - The button.
|
|
11
|
+
* @example <pm-share-button share-title="Prismora" text="One seed, every theme."></pm-share-button>
|
|
12
|
+
*/
|
|
13
|
+
export declare class PmShareButton extends PmElement {
|
|
14
|
+
#private;
|
|
15
|
+
static componentName: string;
|
|
16
|
+
static styles: import("lit").CSSResult[];
|
|
17
|
+
static properties: PropertyDeclarations;
|
|
18
|
+
/** Address to share (default: this page). */
|
|
19
|
+
url?: string;
|
|
20
|
+
/** Title passed to the share sheet (default: the page title). */
|
|
21
|
+
shareTitle?: string;
|
|
22
|
+
/** Short text passed to the share sheet. */
|
|
23
|
+
text?: string;
|
|
24
|
+
/** Button text (default "Share"). */
|
|
25
|
+
label: string;
|
|
26
|
+
variant?: 'solid' | 'soft' | 'outline' | 'ghost';
|
|
27
|
+
size?: 'xs' | 'sm' | 'md' | 'lg';
|
|
28
|
+
/** Show only the icon; the label becomes the accessible name. */
|
|
29
|
+
iconOnly: boolean;
|
|
30
|
+
/** Not interactive. */
|
|
31
|
+
disabled: boolean;
|
|
32
|
+
_copied: boolean;
|
|
33
|
+
constructor();
|
|
34
|
+
/** Share now (also triggered by click). */
|
|
35
|
+
share(): Promise<void>;
|
|
36
|
+
render(): import("lit").TemplateResult<1>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { PmElement, defineElement } from '../base.js';
|
|
3
|
+
import buttonCss from '../styles/button.js';
|
|
4
|
+
/**
|
|
5
|
+
* Share button: opens the device's share sheet (phones, Safari, Edge) and copies the link where there is none.
|
|
6
|
+
*
|
|
7
|
+
* The page address is shared unless `url` is set. After a copy the button says so, visibly and to screen readers.
|
|
8
|
+
* @tag pm-share-button
|
|
9
|
+
* @fires pm-share - Shared or copied. detail: { url, method: 'share' | 'copy' }
|
|
10
|
+
* @fires pm-error - Sharing and copying both failed. detail: { error }
|
|
11
|
+
* @csspart base - The button.
|
|
12
|
+
* @example <pm-share-button share-title="Prismora" text="One seed, every theme."></pm-share-button>
|
|
13
|
+
*/
|
|
14
|
+
export class PmShareButton extends PmElement {
|
|
15
|
+
static { this.componentName = 'share-button'; }
|
|
16
|
+
static { this.styles = [...PmElement.styles, buttonCss, css `:host { display: inline-flex; vertical-align: middle; } .pm-button { gap: 0.4em; }`]; }
|
|
17
|
+
static { this.properties = {
|
|
18
|
+
url: {}, shareTitle: { attribute: 'share-title' }, text: {}, label: {}, variant: { reflect: true }, size: { reflect: true }, iconOnly: { type: Boolean, attribute: 'icon-only' },
|
|
19
|
+
disabled: { type: Boolean, reflect: true }, _copied: { state: true },
|
|
20
|
+
}; }
|
|
21
|
+
#timer;
|
|
22
|
+
constructor() { super(); this.label = 'Share'; this.iconOnly = false; this.disabled = false; this._copied = false; }
|
|
23
|
+
/** Share now (also triggered by click). */
|
|
24
|
+
async share() {
|
|
25
|
+
const url = new URL(this.url || location.href, location.href).href;
|
|
26
|
+
const data = { url, title: this.shareTitle || document.title, ...(this.text ? { text: this.text } : {}) };
|
|
27
|
+
if (navigator.share && (!navigator.canShare || navigator.canShare(data))) {
|
|
28
|
+
try {
|
|
29
|
+
await navigator.share(data);
|
|
30
|
+
this.emit('pm-share', { url, method: 'share' });
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (error?.name === 'AbortError')
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
await navigator.clipboard.writeText(url);
|
|
40
|
+
this._copied = true;
|
|
41
|
+
this.emit('pm-share', { url, method: 'copy' });
|
|
42
|
+
clearTimeout(this.#timer);
|
|
43
|
+
this.#timer = setTimeout(() => (this._copied = false), 2000);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
this.emit('pm-error', { error });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
render() {
|
|
50
|
+
const text = this._copied ? 'Link copied' : this.label;
|
|
51
|
+
const icon = this._copied
|
|
52
|
+
? html `<svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12.5l4.5 4.5L19 7.5"/></svg>`
|
|
53
|
+
: html `<svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><path d="M8.6 13.5l6.8 4M15.4 6.5l-6.8 4"/></svg>`;
|
|
54
|
+
return html `<button type="button" class="pm-button" part="base" data-variant=${this.variant ?? 'outline'} data-tone=${this._copied ? 'success' : 'neutral'}
|
|
55
|
+
data-size=${this.size ?? nothing} ?data-icon=${this.iconOnly} ?disabled=${this.disabled} aria-label=${this.iconOnly ? text : nothing} @click=${this.share}>
|
|
56
|
+
${icon}${this.iconOnly ? nothing : html `<span>${text}</span>`}
|
|
57
|
+
</button><span class="pm-visually-hidden" aria-live="polite">${this._copied ? 'Link copied' : ''}</span>`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
defineElement('pm-share-button', PmShareButton);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type PropertyDeclarations } from 'lit';
|
|
2
|
+
import { PmElement } from '../base.js';
|
|
3
|
+
type Point = {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Signature pad: sign with a finger, pen or mouse. Form-associated; submits a PNG data URL.
|
|
9
|
+
*
|
|
10
|
+
* The ink follows the theme's text colour on screen and is exported dark on a transparent background. Keyboard
|
|
11
|
+
* users cannot draw, so offer a typed-name alternative next to it when a signature is required.
|
|
12
|
+
* @tag pm-signature
|
|
13
|
+
* @fires pm-change - A stroke was finished or the pad was cleared. detail: { empty }
|
|
14
|
+
* @example <pm-signature label="Signature" name="signature" required></pm-signature>
|
|
15
|
+
*/
|
|
16
|
+
export declare class PmSignature extends PmElement {
|
|
17
|
+
#private;
|
|
18
|
+
static componentName: string;
|
|
19
|
+
static formAssociated: boolean;
|
|
20
|
+
static styles: import("lit").CSSResult[];
|
|
21
|
+
static properties: PropertyDeclarations;
|
|
22
|
+
label: string;
|
|
23
|
+
name?: string;
|
|
24
|
+
/** Help text under the pad. */
|
|
25
|
+
hint?: string;
|
|
26
|
+
/** Custom error message. Setting it marks the field invalid. */
|
|
27
|
+
error?: string;
|
|
28
|
+
/** The form cannot be submitted until something is drawn. */
|
|
29
|
+
required: boolean;
|
|
30
|
+
disabled: boolean;
|
|
31
|
+
/** Height of the drawing area: any CSS length (default 10rem). */
|
|
32
|
+
height?: string;
|
|
33
|
+
/** Stroke thickness in pixels (default 2). */
|
|
34
|
+
lineWidth: number;
|
|
35
|
+
/** Text of the clear button. */
|
|
36
|
+
clearLabel: string;
|
|
37
|
+
_empty: boolean;
|
|
38
|
+
constructor();
|
|
39
|
+
/** True while nothing has been drawn. */
|
|
40
|
+
get empty(): boolean;
|
|
41
|
+
/** The strokes, as lists of points in CSS pixels (store these to redraw at any size). */
|
|
42
|
+
get strokes(): Point[][];
|
|
43
|
+
set strokes(v: Point[][]);
|
|
44
|
+
firstUpdated(): void;
|
|
45
|
+
disconnectedCallback(): void;
|
|
46
|
+
formResetCallback(): void;
|
|
47
|
+
formDisabledCallback(d: boolean): void;
|
|
48
|
+
/** Remove everything that was drawn. */
|
|
49
|
+
clear(notify?: boolean): void;
|
|
50
|
+
/** The signature as a PNG data URL (dark ink, transparent background); empty string when nothing is drawn. */
|
|
51
|
+
toDataURL(): string;
|
|
52
|
+
render(): import("lit").TemplateResult<1>;
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { PmElement, defineElement } from '../base.js';
|
|
3
|
+
import { FieldValidity } from '../field-validity.js';
|
|
4
|
+
import buttonCss from '../styles/button.js';
|
|
5
|
+
import extrasCss from '../styles/extras.js';
|
|
6
|
+
import fieldCss from '../styles/field.js';
|
|
7
|
+
/**
|
|
8
|
+
* Signature pad: sign with a finger, pen or mouse. Form-associated; submits a PNG data URL.
|
|
9
|
+
*
|
|
10
|
+
* The ink follows the theme's text colour on screen and is exported dark on a transparent background. Keyboard
|
|
11
|
+
* users cannot draw, so offer a typed-name alternative next to it when a signature is required.
|
|
12
|
+
* @tag pm-signature
|
|
13
|
+
* @fires pm-change - A stroke was finished or the pad was cleared. detail: { empty }
|
|
14
|
+
* @example <pm-signature label="Signature" name="signature" required></pm-signature>
|
|
15
|
+
*/
|
|
16
|
+
export class PmSignature extends PmElement {
|
|
17
|
+
static { this.componentName = 'signature'; }
|
|
18
|
+
static { this.formAssociated = true; }
|
|
19
|
+
static { this.styles = [...PmElement.styles, fieldCss, buttonCss, extrasCss, css `:host { display: block; } :host([hidden]) { display: none; }`]; }
|
|
20
|
+
static { this.properties = {
|
|
21
|
+
label: {}, name: { reflect: true }, hint: {}, error: {}, required: { type: Boolean, reflect: true }, disabled: { type: Boolean, reflect: true },
|
|
22
|
+
height: {}, lineWidth: { type: Number, attribute: 'line-width' }, clearLabel: { attribute: 'clear-label' }, _empty: { state: true },
|
|
23
|
+
}; }
|
|
24
|
+
#internals = this.attachInternals();
|
|
25
|
+
#field = new FieldValidity(this, this.#internals, {
|
|
26
|
+
hint: () => this.hint, error: () => this.error, missing: () => this.required && this._empty && !this.disabled,
|
|
27
|
+
missingMessage: 'Please sign here.', anchor: () => this.renderRoot.querySelector('canvas'),
|
|
28
|
+
});
|
|
29
|
+
#strokes = [];
|
|
30
|
+
#current = null;
|
|
31
|
+
#resize;
|
|
32
|
+
constructor() { super(); this.label = 'Signature'; this.required = false; this.disabled = false; this.lineWidth = 2; this.clearLabel = 'Clear'; this._empty = true; }
|
|
33
|
+
get #canvas() { return this.renderRoot.querySelector('canvas'); }
|
|
34
|
+
/** True while nothing has been drawn. */
|
|
35
|
+
get empty() { return this._empty; }
|
|
36
|
+
/** The strokes, as lists of points in CSS pixels (store these to redraw at any size). */
|
|
37
|
+
get strokes() { return this.#strokes.map((s) => s.map((p) => ({ ...p }))); }
|
|
38
|
+
set strokes(v) { this.#strokes = (v ?? []).map((s) => s.map((p) => ({ x: p.x, y: p.y }))); this._empty = !this.#strokes.length; this.#paint(); this.#commit(false); }
|
|
39
|
+
firstUpdated() {
|
|
40
|
+
this.#resize = new ResizeObserver(() => this.#paint());
|
|
41
|
+
this.#resize.observe(this.#canvas);
|
|
42
|
+
}
|
|
43
|
+
disconnectedCallback() { super.disconnectedCallback(); this.#resize?.disconnect(); }
|
|
44
|
+
formResetCallback() { this.clear(false); this.#field.reset(); }
|
|
45
|
+
formDisabledCallback(d) { this.disabled = d; }
|
|
46
|
+
/** Remove everything that was drawn. */
|
|
47
|
+
clear(notify = true) {
|
|
48
|
+
this.#strokes = [];
|
|
49
|
+
this.#current = null;
|
|
50
|
+
this._empty = true;
|
|
51
|
+
this.#paint();
|
|
52
|
+
this.#commit(notify);
|
|
53
|
+
}
|
|
54
|
+
/** The signature as a PNG data URL (dark ink, transparent background); empty string when nothing is drawn. */
|
|
55
|
+
toDataURL() {
|
|
56
|
+
if (this._empty)
|
|
57
|
+
return '';
|
|
58
|
+
const src = this.#canvas;
|
|
59
|
+
const out = document.createElement('canvas');
|
|
60
|
+
out.width = src.width;
|
|
61
|
+
out.height = src.height;
|
|
62
|
+
this.#draw(out.getContext('2d'), src.width / Math.max(1, src.clientWidth), '#111');
|
|
63
|
+
return out.toDataURL('image/png');
|
|
64
|
+
}
|
|
65
|
+
#commit(notify) {
|
|
66
|
+
this.#internals.setFormValue(this._empty ? null : this.toDataURL());
|
|
67
|
+
if (notify) {
|
|
68
|
+
this.#field.touch();
|
|
69
|
+
this.emit('pm-change', { empty: this._empty });
|
|
70
|
+
}
|
|
71
|
+
this.requestUpdate();
|
|
72
|
+
}
|
|
73
|
+
#draw(ctx, scale, colour) {
|
|
74
|
+
ctx.setTransform(scale, 0, 0, scale, 0, 0);
|
|
75
|
+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
76
|
+
ctx.strokeStyle = colour;
|
|
77
|
+
ctx.fillStyle = colour;
|
|
78
|
+
ctx.lineWidth = this.lineWidth;
|
|
79
|
+
ctx.lineCap = 'round';
|
|
80
|
+
ctx.lineJoin = 'round';
|
|
81
|
+
for (const s of this.#current ? [...this.#strokes, this.#current] : this.#strokes) {
|
|
82
|
+
if (s.length === 1) {
|
|
83
|
+
ctx.beginPath();
|
|
84
|
+
ctx.arc(s[0].x, s[0].y, this.lineWidth / 2, 0, Math.PI * 2);
|
|
85
|
+
ctx.fill();
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
ctx.beginPath();
|
|
89
|
+
ctx.moveTo(s[0].x, s[0].y);
|
|
90
|
+
// Smooth the line through the midpoints of consecutive points.
|
|
91
|
+
for (let i = 1; i < s.length - 1; i++)
|
|
92
|
+
ctx.quadraticCurveTo(s[i].x, s[i].y, (s[i].x + s[i + 1].x) / 2, (s[i].y + s[i + 1].y) / 2);
|
|
93
|
+
ctx.lineTo(s[s.length - 1].x, s[s.length - 1].y);
|
|
94
|
+
ctx.stroke();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
#paint() {
|
|
98
|
+
const c = this.renderRoot.querySelector('canvas');
|
|
99
|
+
if (!c || !c.clientWidth)
|
|
100
|
+
return;
|
|
101
|
+
const ratio = Math.min(3, devicePixelRatio || 1);
|
|
102
|
+
const w = Math.round(c.clientWidth * ratio), h = Math.round(c.clientHeight * ratio);
|
|
103
|
+
if (c.width !== w || c.height !== h) {
|
|
104
|
+
c.width = w;
|
|
105
|
+
c.height = h;
|
|
106
|
+
}
|
|
107
|
+
this.#draw(c.getContext('2d'), ratio, getComputedStyle(c).color);
|
|
108
|
+
}
|
|
109
|
+
#point(e) {
|
|
110
|
+
const r = this.#canvas.getBoundingClientRect();
|
|
111
|
+
return { x: Math.round((e.clientX - r.left) * 10) / 10, y: Math.round((e.clientY - r.top) * 10) / 10 };
|
|
112
|
+
}
|
|
113
|
+
#down = (e) => {
|
|
114
|
+
if (this.disabled || (e.pointerType === 'mouse' && e.button !== 0))
|
|
115
|
+
return;
|
|
116
|
+
e.preventDefault();
|
|
117
|
+
this.#canvas.setPointerCapture(e.pointerId);
|
|
118
|
+
this.#current = [this.#point(e)];
|
|
119
|
+
this.#paint();
|
|
120
|
+
};
|
|
121
|
+
#move = (e) => {
|
|
122
|
+
if (!this.#current)
|
|
123
|
+
return;
|
|
124
|
+
for (const ev of e.getCoalescedEvents?.().length ? e.getCoalescedEvents() : [e])
|
|
125
|
+
this.#current.push(this.#point(ev));
|
|
126
|
+
this.#paint();
|
|
127
|
+
};
|
|
128
|
+
#up = () => {
|
|
129
|
+
if (!this.#current)
|
|
130
|
+
return;
|
|
131
|
+
this.#strokes.push(this.#current);
|
|
132
|
+
this.#current = null;
|
|
133
|
+
this._empty = false;
|
|
134
|
+
this.#commit(true);
|
|
135
|
+
};
|
|
136
|
+
render() {
|
|
137
|
+
const invalid = this.#field.invalid;
|
|
138
|
+
return html `<div class="pm-field" part="field">
|
|
139
|
+
<span class="pm-label" id="l" part="label" ?data-required=${this.required}>${this.label}</span>
|
|
140
|
+
<div class="pm-signature" part="base" ?data-invalid=${invalid} ?data-disabled=${this.disabled} style=${this.height ? `--_height: ${this.height}` : nothing}>
|
|
141
|
+
<canvas part="canvas" role="img" tabindex="0" aria-labelledby="l" aria-describedby=${this.#field.describedBy ?? nothing}
|
|
142
|
+
aria-description=${this._empty ? 'Empty. Draw with a pointer or finger.' : 'Signed.'}
|
|
143
|
+
@pointerdown=${this.#down} @pointermove=${this.#move} @pointerup=${this.#up} @pointercancel=${this.#up}></canvas>
|
|
144
|
+
<span class="pm-signature-line"></span>
|
|
145
|
+
${this._empty ? html `<span class="pm-signature-hint" aria-hidden="true">Sign above the line</span>` : nothing}
|
|
146
|
+
${this._empty || this.disabled ? nothing : html `<button type="button" class="pm-button pm-signature-clear" data-variant="ghost" data-tone="neutral" data-size="sm" part="clear" @click=${() => this.clear()}>${this.clearLabel}<span class="pm-visually-hidden"> ${this.label}</span></button>`}
|
|
147
|
+
</div>
|
|
148
|
+
${this.#field.messages()}
|
|
149
|
+
</div>`;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
defineElement('pm-signature', PmSignature);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type PropertyDeclarations } from 'lit';
|
|
2
|
+
import { PmElement } from '../base.js';
|
|
3
|
+
/**
|
|
4
|
+
* Loading placeholder.
|
|
5
|
+
* @tag pm-skeleton
|
|
6
|
+
* @example <pm-skeleton shape="text" lines="3"></pm-skeleton>
|
|
7
|
+
*/
|
|
8
|
+
export declare class PmSkeleton extends PmElement {
|
|
9
|
+
static componentName: string;
|
|
10
|
+
static styles: import("lit").CSSResult[];
|
|
11
|
+
static properties: PropertyDeclarations;
|
|
12
|
+
shape?: 'rect' | 'text' | 'circle';
|
|
13
|
+
/** Number of text lines to imitate. */
|
|
14
|
+
lines: number;
|
|
15
|
+
/** Width as a CSS length, e.g. "12rem" or "60%". */
|
|
16
|
+
width?: string;
|
|
17
|
+
/** Height as a CSS length. */
|
|
18
|
+
height?: string;
|
|
19
|
+
constructor();
|
|
20
|
+
render(): import("lit").TemplateResult<1>;
|
|
21
|
+
}
|