@prismora/elements 0.0.0-stage → 0.1.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.
- 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,289 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { PmElement, SafeHTMLElement, defineElement } from '../base.js';
|
|
3
|
+
import { RemoteRequest, buildUrl, fetchJson, field, pick } from '../remote.js';
|
|
4
|
+
import { closeTopLayer } from '../top-layer.js';
|
|
5
|
+
import commandCss from '../styles/command.js';
|
|
6
|
+
import dialogCss from '../styles/dialog.js';
|
|
7
|
+
import kbdCss from '../styles/kbd.js';
|
|
8
|
+
import listboxCss from '../styles/listbox.js';
|
|
9
|
+
import overlayCss from '../styles/overlay.js';
|
|
10
|
+
/** Fuzzy score: substring beats subsequence; 0 = no match. */
|
|
11
|
+
export function commandScore(item, query) {
|
|
12
|
+
const q = query.trim().toLowerCase();
|
|
13
|
+
if (!q)
|
|
14
|
+
return 1;
|
|
15
|
+
const hay = [item.label, ...(item.keywords ?? [])].join(' ').toLowerCase();
|
|
16
|
+
const i = hay.indexOf(q);
|
|
17
|
+
if (i === 0)
|
|
18
|
+
return 4;
|
|
19
|
+
if (i > 0)
|
|
20
|
+
return hay[i - 1] === ' ' ? 3 : 2;
|
|
21
|
+
let k = 0;
|
|
22
|
+
for (const ch of hay)
|
|
23
|
+
if (ch === q[k])
|
|
24
|
+
k++;
|
|
25
|
+
return k === q.length ? 1 : 0;
|
|
26
|
+
}
|
|
27
|
+
const isMac = () => /Mac|iP(hone|ad|od)/.test(globalThis.navigator?.platform ?? '');
|
|
28
|
+
/**
|
|
29
|
+
* Command palette (⌘K / Ctrl+K): a modal search over actions and pages, grouped, fuzzy-matched, keyboard driven.
|
|
30
|
+
*
|
|
31
|
+
* Items come from child `<pm-command-item>` elements or the `items` property. Open with the shortcut, `el.show()`,
|
|
32
|
+
* or an invoker: `<pm-button commandfor="cmd" command="--open">`.
|
|
33
|
+
*
|
|
34
|
+
* Server search: set `src` (`src="/api/search?q={query}"`, with `items-path`, `value-key`, `label-key`,
|
|
35
|
+
* `group-key`, `href-key`) or the `loader` property (`async (query, { signal }) => items`, `mapItem` to format).
|
|
36
|
+
* Local commands still match instantly; server results follow under their own group (`results-group`), debounced,
|
|
37
|
+
* with loading and error (Retry) states.
|
|
38
|
+
* @tag pm-command
|
|
39
|
+
* @fires pm-select - An item was chosen. detail: { value, item }
|
|
40
|
+
* @fires pm-open - Opened.
|
|
41
|
+
* @fires pm-close - Closed.
|
|
42
|
+
* @fires pm-load - Server results arrived. detail: { query, items }
|
|
43
|
+
* @fires pm-load-error - A server search failed. detail: { query, error }
|
|
44
|
+
* @example <pm-command><pm-command-item value="new" group="Actions" shortcut="⌘ N">New project</pm-command-item><pm-command-item value="settings" group="Pages" href="/settings">Settings</pm-command-item></pm-command>
|
|
45
|
+
*/
|
|
46
|
+
export class PmCommand extends PmElement {
|
|
47
|
+
static { this.componentName = 'command'; }
|
|
48
|
+
static { this.styles = [...PmElement.styles, overlayCss, dialogCss, listboxCss, kbdCss, commandCss, css `:host { display: contents; }`]; }
|
|
49
|
+
static { this.properties = {
|
|
50
|
+
open: { type: Boolean, reflect: true }, items: { attribute: false }, placeholder: {}, label: {}, shortcut: {}, emptyText: { attribute: 'empty-text' },
|
|
51
|
+
src: {}, loader: { attribute: false }, mapItem: { attribute: false }, itemsPath: { attribute: 'items-path' }, valueKey: { attribute: 'value-key' },
|
|
52
|
+
labelKey: { attribute: 'label-key' }, groupKey: { attribute: 'group-key' }, hrefKey: { attribute: 'href-key' }, resultsGroup: { attribute: 'results-group' },
|
|
53
|
+
debounce: { type: Number }, minChars: { type: Number, attribute: 'min-chars' },
|
|
54
|
+
_query: { state: true }, _active: { state: true }, _remote: { state: true },
|
|
55
|
+
}; }
|
|
56
|
+
#mo;
|
|
57
|
+
#returnFocus;
|
|
58
|
+
constructor() {
|
|
59
|
+
super();
|
|
60
|
+
this.open = false;
|
|
61
|
+
this.items = [];
|
|
62
|
+
this.placeholder = 'Type a command or search…';
|
|
63
|
+
this.label = 'Command palette';
|
|
64
|
+
this.resultsGroup = 'Results';
|
|
65
|
+
this.debounce = 200;
|
|
66
|
+
this.minChars = 1;
|
|
67
|
+
this._remote = [];
|
|
68
|
+
this.shortcut = 'mod+k';
|
|
69
|
+
this.emptyText = 'No results';
|
|
70
|
+
this._query = '';
|
|
71
|
+
this._active = 0;
|
|
72
|
+
// Invoker commands: <pm-button commandfor="cmd" command="--open|--close|--toggle">
|
|
73
|
+
this.addEventListener('command', (e) => {
|
|
74
|
+
const cmd = e.command;
|
|
75
|
+
if (cmd === '--open' || cmd === 'show-modal')
|
|
76
|
+
this.show();
|
|
77
|
+
else if (cmd === '--close' || cmd === 'close')
|
|
78
|
+
this.hide();
|
|
79
|
+
else if (cmd === '--toggle')
|
|
80
|
+
this.toggle();
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
connectedCallback() {
|
|
84
|
+
super.connectedCallback();
|
|
85
|
+
this.#read();
|
|
86
|
+
this.#mo = new MutationObserver(() => this.#read());
|
|
87
|
+
this.#mo.observe(this, { childList: true, subtree: true, attributes: true, characterData: true });
|
|
88
|
+
document.addEventListener('keydown', this.#onGlobalKey);
|
|
89
|
+
}
|
|
90
|
+
disconnectedCallback() { super.disconnectedCallback(); this.#mo?.disconnect(); document.removeEventListener('keydown', this.#onGlobalKey); }
|
|
91
|
+
#search = new RemoteRequest(this, {
|
|
92
|
+
debounce: () => this.debounce,
|
|
93
|
+
run: async (query, ctx) => {
|
|
94
|
+
const data = this.loader ? await this.loader(query, ctx) : await fetchJson(buildUrl(this.src, { query, q: query }), ctx.signal);
|
|
95
|
+
const list = pick(data, this.itemsPath);
|
|
96
|
+
if (!Array.isArray(list))
|
|
97
|
+
throw new Error(`Expected a list${this.itemsPath ? ` at "${this.itemsPath}"` : ''} in the search response`);
|
|
98
|
+
return list.map((item) => this.mapItem?.(item) ?? this.#toItem(item));
|
|
99
|
+
},
|
|
100
|
+
done: (items, query) => { this._remote = items; this.emit('pm-load', { query, items }); },
|
|
101
|
+
failed: (error, query) => this.emit('pm-load-error', { query, error }),
|
|
102
|
+
});
|
|
103
|
+
#toItem(item) {
|
|
104
|
+
if (item == null || typeof item !== 'object')
|
|
105
|
+
return { value: String(item), label: String(item), group: this.resultsGroup };
|
|
106
|
+
const value = String(field(item, [this.valueKey, 'value', 'id']) ?? '');
|
|
107
|
+
const href = field(item, [this.hrefKey, 'href', 'url']);
|
|
108
|
+
return {
|
|
109
|
+
value, label: String(field(item, [this.labelKey, 'label', 'name', 'title']) ?? value),
|
|
110
|
+
group: String(field(item, [this.groupKey, 'group']) ?? this.resultsGroup),
|
|
111
|
+
...(href ? { href: String(href) } : {}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
#onQuery(q) {
|
|
115
|
+
this._query = q;
|
|
116
|
+
this._active = 0;
|
|
117
|
+
if (!this.loader && !this.src)
|
|
118
|
+
return;
|
|
119
|
+
if (q.trim().length >= this.minChars)
|
|
120
|
+
this.#search.request(q.trim());
|
|
121
|
+
else {
|
|
122
|
+
this.#search.cancel();
|
|
123
|
+
this._remote = [];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
#read() {
|
|
127
|
+
const els = [...this.querySelectorAll('pm-command-item')];
|
|
128
|
+
if (!els.length)
|
|
129
|
+
return;
|
|
130
|
+
this.items = els.map((e) => ({
|
|
131
|
+
value: e.getAttribute('value') ?? e.textContent.trim(), label: e.textContent.trim(), group: e.getAttribute('group') ?? undefined,
|
|
132
|
+
keywords: e.getAttribute('keywords')?.split(/[\s,]+/).filter(Boolean), shortcut: e.getAttribute('shortcut') ?? undefined,
|
|
133
|
+
href: e.getAttribute('href') ?? undefined, disabled: e.hasAttribute('disabled'),
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
#onGlobalKey = (e) => {
|
|
137
|
+
if (!this.shortcut || e.defaultPrevented)
|
|
138
|
+
return;
|
|
139
|
+
const parts = this.shortcut.toLowerCase().split('+');
|
|
140
|
+
const key = parts.pop();
|
|
141
|
+
const mod = parts.includes('mod');
|
|
142
|
+
const modOk = mod ? (isMac() ? e.metaKey : e.ctrlKey) : !(e.metaKey || e.ctrlKey);
|
|
143
|
+
if (modOk && e.key.toLowerCase() === key && e.shiftKey === parts.includes('shift') && e.altKey === parts.includes('alt')) {
|
|
144
|
+
e.preventDefault();
|
|
145
|
+
this.open ? this.hide() : this.show();
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
show() { this.open = true; }
|
|
149
|
+
hide() { this.open = false; }
|
|
150
|
+
toggle() { this.open = !this.open; }
|
|
151
|
+
get #dialog() { return this.renderRoot.querySelector('dialog'); }
|
|
152
|
+
get #results() {
|
|
153
|
+
return this.items.map((item, i) => ({ item, score: commandScore(item, this._query), i }))
|
|
154
|
+
.filter((r) => r.score > 0)
|
|
155
|
+
.sort((a, b) => (this._query ? b.score - a.score : 0) || a.i - b.i)
|
|
156
|
+
.map((r) => r.item)
|
|
157
|
+
// Server results are already filtered and ranked by the server.
|
|
158
|
+
.concat(this._query.trim() ? this._remote.map((it) => ({ ...it, group: it.group ?? this.resultsGroup })) : []);
|
|
159
|
+
}
|
|
160
|
+
/** Results ordered by group (first appearance), for display and keyboard order. */
|
|
161
|
+
get #ordered() {
|
|
162
|
+
const groups = new Map();
|
|
163
|
+
for (const it of this.#results) {
|
|
164
|
+
const g = it.group ?? '';
|
|
165
|
+
if (!groups.has(g))
|
|
166
|
+
groups.set(g, []);
|
|
167
|
+
groups.get(g).push(it);
|
|
168
|
+
}
|
|
169
|
+
return [...groups.entries()];
|
|
170
|
+
}
|
|
171
|
+
updated(changed) {
|
|
172
|
+
if (!changed.has('open')) {
|
|
173
|
+
this.renderRoot.querySelector('[data-active]')?.scrollIntoView({ block: 'nearest' });
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const d = this.#dialog;
|
|
177
|
+
if (!d)
|
|
178
|
+
return;
|
|
179
|
+
if (this.open && !d.open) {
|
|
180
|
+
this.#returnFocus = document.activeElement;
|
|
181
|
+
this._query = '';
|
|
182
|
+
this._active = 0;
|
|
183
|
+
this._remote = [];
|
|
184
|
+
d.showModal();
|
|
185
|
+
this.renderRoot.querySelector('input')?.focus();
|
|
186
|
+
this.emit('pm-open');
|
|
187
|
+
}
|
|
188
|
+
else if (!this.open && d.open) {
|
|
189
|
+
closeTopLayer(d, () => d.close());
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
#onClose = () => {
|
|
193
|
+
this.open = false;
|
|
194
|
+
this.emit('pm-close');
|
|
195
|
+
this.#returnFocus?.focus?.();
|
|
196
|
+
};
|
|
197
|
+
#choose(item) {
|
|
198
|
+
if (item.disabled)
|
|
199
|
+
return;
|
|
200
|
+
const ok = this.emit('pm-select', { value: item.value, item }, { cancelable: true });
|
|
201
|
+
this.hide();
|
|
202
|
+
if (ok && item.href)
|
|
203
|
+
location.assign(item.href);
|
|
204
|
+
}
|
|
205
|
+
#onKey(e) {
|
|
206
|
+
const flat = this.#ordered.flatMap(([, items]) => items);
|
|
207
|
+
const enabled = (i) => flat[i] && !flat[i].disabled;
|
|
208
|
+
const move = (dir) => {
|
|
209
|
+
for (let n = 1; n <= flat.length; n++) {
|
|
210
|
+
const i = (this._active + dir * n + flat.length) % flat.length;
|
|
211
|
+
if (enabled(i)) {
|
|
212
|
+
this._active = i;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
if (e.key === 'ArrowDown') {
|
|
218
|
+
e.preventDefault();
|
|
219
|
+
move(1);
|
|
220
|
+
}
|
|
221
|
+
else if (e.key === 'ArrowUp') {
|
|
222
|
+
e.preventDefault();
|
|
223
|
+
move(-1);
|
|
224
|
+
}
|
|
225
|
+
else if (e.key === 'Home' && e.ctrlKey) {
|
|
226
|
+
e.preventDefault();
|
|
227
|
+
this._active = 0;
|
|
228
|
+
}
|
|
229
|
+
else if (e.key === 'Enter') {
|
|
230
|
+
e.preventDefault();
|
|
231
|
+
const it = flat[this._active];
|
|
232
|
+
if (it)
|
|
233
|
+
this.#choose(it);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
render() {
|
|
237
|
+
const ordered = this.#ordered;
|
|
238
|
+
let idx = -1;
|
|
239
|
+
const flat = ordered.flatMap(([, items]) => items);
|
|
240
|
+
if (this._active >= flat.length)
|
|
241
|
+
this._active = Math.max(0, flat.length - 1);
|
|
242
|
+
const activeId = flat.length ? `cmd-opt-${this._active}` : undefined;
|
|
243
|
+
return html `<dialog class="pm-surface pm-dialog pm-command" part="dialog" aria-label=${this.label} @close=${this.#onClose}
|
|
244
|
+
@click=${(e) => { if (e.target === e.currentTarget)
|
|
245
|
+
this.hide(); }}>
|
|
246
|
+
<div class="pm-command-search">
|
|
247
|
+
<svg viewBox="0 0 24 24" width="1.15em" height="1.15em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="M20 20l-3.5-3.5"/></svg>
|
|
248
|
+
<input part="input" type="text" role="combobox" aria-expanded="true" aria-controls="cmd-list" aria-autocomplete="list" autocomplete="off" spellcheck="false"
|
|
249
|
+
aria-label=${this.label} placeholder=${this.placeholder} .value=${this._query} aria-activedescendant=${activeId ?? nothing}
|
|
250
|
+
@input=${(e) => this.#onQuery(e.target.value)} @keydown=${this.#onKey} />
|
|
251
|
+
</div>
|
|
252
|
+
<div class="pm-listbox" role="listbox" id="cmd-list" aria-label="Results">
|
|
253
|
+
${ordered.map(([group, items], gi) => html `<div role="group" aria-labelledby=${group ? `cmd-g-${gi}` : nothing}>
|
|
254
|
+
${group ? html `<div class="pm-listbox-group" id=${`cmd-g-${gi}`} role="presentation">${group}</div>` : nothing}
|
|
255
|
+
${items.map((it) => {
|
|
256
|
+
idx++;
|
|
257
|
+
const i = idx;
|
|
258
|
+
return html `<div class="pm-option" role="option" id=${`cmd-opt-${i}`} aria-selected=${i === this._active ? 'true' : 'false'}
|
|
259
|
+
aria-disabled=${it.disabled ? 'true' : nothing} ?data-active=${i === this._active}
|
|
260
|
+
@pointermove=${() => { if (this._active !== i && !it.disabled)
|
|
261
|
+
this._active = i; }} @click=${() => this.#choose(it)}>
|
|
262
|
+
<span class="pm-option-label">${it.label}</span>${it.shortcut ? html `<kbd class="pm-kbd" aria-hidden="true">${it.shortcut}</kbd>` : nothing}</div>`;
|
|
263
|
+
})}
|
|
264
|
+
</div>`)}
|
|
265
|
+
</div>
|
|
266
|
+
${this.#search.pending
|
|
267
|
+
? html `<div class="pm-listbox-empty" role="status">Searching…</div>`
|
|
268
|
+
: this.#search.error
|
|
269
|
+
? html `<div class="pm-listbox-empty" role="status" data-error>Couldn't load results. <button type="button" class="pm-listbox-retry" @click=${() => this.#search.retry()}>Retry</button></div>`
|
|
270
|
+
: flat.length ? nothing : html `<div class="pm-listbox-empty" role="status">${this.emptyText}</div>`}
|
|
271
|
+
<div class="pm-command-foot" aria-hidden="true"><span><kbd class="pm-kbd">↑</kbd> <kbd class="pm-kbd">↓</kbd> navigate</span><span><kbd class="pm-kbd">↵</kbd> select</span><span><kbd class="pm-kbd">esc</kbd> close</span></div>
|
|
272
|
+
</dialog>`;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* An action in a command palette (data only, not rendered itself).
|
|
277
|
+
* @tag pm-command-item
|
|
278
|
+
* @attr {string} value - Value reported in pm-select (defaults to the text).
|
|
279
|
+
* @attr {string} group - Group heading.
|
|
280
|
+
* @attr {string} keywords - Extra search words, space or comma separated.
|
|
281
|
+
* @attr {string} shortcut - Shortcut hint shown on the right, e.g. "Ctrl N".
|
|
282
|
+
* @attr {string} href - Navigate here when chosen.
|
|
283
|
+
* @attr {boolean} disabled
|
|
284
|
+
* @example <pm-command-item value="logout" group="Account" keywords="sign out">Log out</pm-command-item>
|
|
285
|
+
*/
|
|
286
|
+
export class PmCommandItem extends SafeHTMLElement {
|
|
287
|
+
}
|
|
288
|
+
defineElement('pm-command', PmCommand);
|
|
289
|
+
defineElement('pm-command-item', PmCommandItem);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SafeHTMLElement } from '../base.js';
|
|
2
|
+
import type { PmMenu } from './menu.js';
|
|
3
|
+
import './menu.js';
|
|
4
|
+
/**
|
|
5
|
+
* Context menu: right-click (or long-press, Shift+F10, the Menu key) inside this element opens the `pm-menu`
|
|
6
|
+
* referenced by `menu` at the pointer. The region itself stays fully interactive.
|
|
7
|
+
* @attr {boolean} mobile - Native mobile pattern on phones: opens as a swipeable bottom sheet (inherits from ancestors such as `<html mobile>`; `mobile="false"` opts out).
|
|
8
|
+
* @tag pm-context-menu
|
|
9
|
+
* @attr {string} menu - id of the `pm-menu` to open.
|
|
10
|
+
* @attr {boolean} disabled - Use the browser's own context menu instead.
|
|
11
|
+
* @slot - The region that owns the menu.
|
|
12
|
+
* @fires pm-open - The menu opened. detail: { x, y, target }
|
|
13
|
+
* @example <pm-context-menu menu="file-menu"><div>Right-click me</div></pm-context-menu>
|
|
14
|
+
* <pm-menu id="file-menu"><button role="menuitem">Rename</button><button role="menuitem">Delete</button></pm-menu>
|
|
15
|
+
*/
|
|
16
|
+
export declare class PmContextMenu extends SafeHTMLElement {
|
|
17
|
+
#private;
|
|
18
|
+
connectedCallback(): void;
|
|
19
|
+
disconnectedCallback(): void;
|
|
20
|
+
get menu(): PmMenu | null;
|
|
21
|
+
/** Open the menu at viewport coordinates. */
|
|
22
|
+
openAt(x: number, y: number, target?: EventTarget | null): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { SafeHTMLElement, defineElement } from '../base.js';
|
|
2
|
+
import { isCompact } from '../sheet.js';
|
|
3
|
+
import './menu.js';
|
|
4
|
+
/**
|
|
5
|
+
* Context menu: right-click (or long-press, Shift+F10, the Menu key) inside this element opens the `pm-menu`
|
|
6
|
+
* referenced by `menu` at the pointer. The region itself stays fully interactive.
|
|
7
|
+
* @attr {boolean} mobile - Native mobile pattern on phones: opens as a swipeable bottom sheet (inherits from ancestors such as `<html mobile>`; `mobile="false"` opts out).
|
|
8
|
+
* @tag pm-context-menu
|
|
9
|
+
* @attr {string} menu - id of the `pm-menu` to open.
|
|
10
|
+
* @attr {boolean} disabled - Use the browser's own context menu instead.
|
|
11
|
+
* @slot - The region that owns the menu.
|
|
12
|
+
* @fires pm-open - The menu opened. detail: { x, y, target }
|
|
13
|
+
* @example <pm-context-menu menu="file-menu"><div>Right-click me</div></pm-context-menu>
|
|
14
|
+
* <pm-menu id="file-menu"><button role="menuitem">Rename</button><button role="menuitem">Delete</button></pm-menu>
|
|
15
|
+
*/
|
|
16
|
+
export class PmContextMenu extends SafeHTMLElement {
|
|
17
|
+
#timer;
|
|
18
|
+
#returnFocus;
|
|
19
|
+
connectedCallback() {
|
|
20
|
+
this.style.display ||= 'block';
|
|
21
|
+
this.addEventListener('contextmenu', this.#onContext);
|
|
22
|
+
this.addEventListener('keydown', this.#onKey);
|
|
23
|
+
this.addEventListener('pointerdown', this.#onDown);
|
|
24
|
+
if (!this.hasAttribute('aria-haspopup'))
|
|
25
|
+
this.setAttribute('aria-haspopup', 'menu');
|
|
26
|
+
}
|
|
27
|
+
disconnectedCallback() {
|
|
28
|
+
this.removeEventListener('contextmenu', this.#onContext);
|
|
29
|
+
this.removeEventListener('keydown', this.#onKey);
|
|
30
|
+
this.removeEventListener('pointerdown', this.#onDown);
|
|
31
|
+
}
|
|
32
|
+
get menu() {
|
|
33
|
+
const id = this.getAttribute('menu');
|
|
34
|
+
return (id ? this.getRootNode() : null)?.querySelector?.(`#${CSS.escape(id)}`) ?? null;
|
|
35
|
+
}
|
|
36
|
+
/** Open the menu at viewport coordinates. */
|
|
37
|
+
openAt(x, y, target = this) {
|
|
38
|
+
const m = this.menu;
|
|
39
|
+
if (!m)
|
|
40
|
+
return;
|
|
41
|
+
this.#returnFocus = this.getRootNode().activeElement;
|
|
42
|
+
try {
|
|
43
|
+
if (m.matches(':popover-open'))
|
|
44
|
+
m.hidePopover();
|
|
45
|
+
}
|
|
46
|
+
catch { /* not open */ }
|
|
47
|
+
// A `mobile` setting on the context menu (or its ancestors) carries over to its menu.
|
|
48
|
+
if (isCompact(this) && !m.hasAttribute('mobile'))
|
|
49
|
+
m.setAttribute('mobile', '');
|
|
50
|
+
if (isCompact(m)) {
|
|
51
|
+
// Phones: the menu opens as a bottom action sheet (pm-menu handles it).
|
|
52
|
+
m.showPopover();
|
|
53
|
+
this.dispatchEvent(new CustomEvent('pm-open', { detail: { x, y, target }, bubbles: true, composed: true }));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
m.style.position = 'fixed';
|
|
57
|
+
m.style.margin = '0';
|
|
58
|
+
m.style.inset = 'auto';
|
|
59
|
+
m.style.left = `${x}px`;
|
|
60
|
+
m.style.top = `${y}px`;
|
|
61
|
+
m.showPopover();
|
|
62
|
+
const r = m.getBoundingClientRect();
|
|
63
|
+
const vw = document.documentElement.clientWidth;
|
|
64
|
+
const vh = document.documentElement.clientHeight;
|
|
65
|
+
if (r.right > vw - 8)
|
|
66
|
+
m.style.left = `${Math.max(8, x - r.width)}px`;
|
|
67
|
+
if (r.bottom > vh - 8)
|
|
68
|
+
m.style.top = `${Math.max(8, y - r.height)}px`;
|
|
69
|
+
const restore = (e) => {
|
|
70
|
+
if (e.newState !== 'closed')
|
|
71
|
+
return;
|
|
72
|
+
m.removeEventListener('toggle', restore);
|
|
73
|
+
if (this.#returnFocus && this.contains(this.#returnFocus) && !m.contains(document.activeElement))
|
|
74
|
+
this.#returnFocus.focus();
|
|
75
|
+
};
|
|
76
|
+
m.addEventListener('toggle', restore);
|
|
77
|
+
this.dispatchEvent(new CustomEvent('pm-open', { detail: { x, y, target }, bubbles: true, composed: true }));
|
|
78
|
+
}
|
|
79
|
+
#onContext = (e) => {
|
|
80
|
+
if (!this.menu || this.hasAttribute('disabled'))
|
|
81
|
+
return;
|
|
82
|
+
e.preventDefault();
|
|
83
|
+
if (e.button === -1 || (e.clientX === 0 && e.clientY === 0)) {
|
|
84
|
+
const r = e.target.getBoundingClientRect();
|
|
85
|
+
this.openAt(r.left + 8, r.bottom);
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
this.openAt(e.clientX, e.clientY, e.target);
|
|
89
|
+
};
|
|
90
|
+
#onKey = (e) => {
|
|
91
|
+
if ((e.key === 'F10' && e.shiftKey) || e.key === 'ContextMenu') {
|
|
92
|
+
if (!this.menu || this.hasAttribute('disabled'))
|
|
93
|
+
return;
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
const r = e.target.getBoundingClientRect();
|
|
96
|
+
this.openAt(r.left + 8, r.top + Math.min(r.height, 32), e.target);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
/** Long-press on touch. */
|
|
100
|
+
#onDown = (e) => {
|
|
101
|
+
if (e.pointerType !== 'touch')
|
|
102
|
+
return;
|
|
103
|
+
clearTimeout(this.#timer);
|
|
104
|
+
const cancel = () => { clearTimeout(this.#timer); removeEventListener('pointerup', cancel); removeEventListener('pointermove', cancel); };
|
|
105
|
+
this.#timer = setTimeout(() => { cancel(); this.openAt(e.clientX, e.clientY, e.target); }, 550);
|
|
106
|
+
addEventListener('pointerup', cancel, { once: true });
|
|
107
|
+
addEventListener('pointermove', cancel, { once: true });
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
defineElement('pm-context-menu', PmContextMenu);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type PropertyDeclarations } from 'lit';
|
|
2
|
+
import { PmElement } from '../base.js';
|
|
3
|
+
/**
|
|
4
|
+
* Copies text to the clipboard and confirms it (visually and to screen readers).
|
|
5
|
+
* @tag pm-copy-button
|
|
6
|
+
* @fires pm-copy - After a successful copy. detail: { value }
|
|
7
|
+
* @fires pm-error - Clipboard unavailable or denied. detail: { error }
|
|
8
|
+
* @example <pm-copy-button value="bun add @prismora/css"></pm-copy-button>
|
|
9
|
+
*/
|
|
10
|
+
export declare class PmCopyButton extends PmElement {
|
|
11
|
+
#private;
|
|
12
|
+
static componentName: string;
|
|
13
|
+
static styles: import("lit").CSSResult[];
|
|
14
|
+
static properties: PropertyDeclarations;
|
|
15
|
+
/** Text to copy. */
|
|
16
|
+
value?: string;
|
|
17
|
+
/** id of an element whose text (or value) is copied instead. */
|
|
18
|
+
for?: string;
|
|
19
|
+
label: string;
|
|
20
|
+
/** Text shown (and announced) after copying. */
|
|
21
|
+
copiedLabel: string;
|
|
22
|
+
variant?: 'solid' | 'soft' | 'outline' | 'ghost';
|
|
23
|
+
size?: 'xs' | 'sm' | 'md' | 'lg';
|
|
24
|
+
/** Show the text label next to the icon. */
|
|
25
|
+
showLabel: boolean;
|
|
26
|
+
/** Not interactive. */
|
|
27
|
+
disabled: boolean;
|
|
28
|
+
_copied: boolean;
|
|
29
|
+
constructor();
|
|
30
|
+
render(): import("lit").TemplateResult<1>;
|
|
31
|
+
/** Copy now (also triggered by click). */
|
|
32
|
+
copy(): Promise<void>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { PmElement, defineElement } from '../base.js';
|
|
3
|
+
import buttonCss from '../styles/button.js';
|
|
4
|
+
/**
|
|
5
|
+
* Copies text to the clipboard and confirms it (visually and to screen readers).
|
|
6
|
+
* @tag pm-copy-button
|
|
7
|
+
* @fires pm-copy - After a successful copy. detail: { value }
|
|
8
|
+
* @fires pm-error - Clipboard unavailable or denied. detail: { error }
|
|
9
|
+
* @example <pm-copy-button value="bun add @prismora/css"></pm-copy-button>
|
|
10
|
+
*/
|
|
11
|
+
export class PmCopyButton extends PmElement {
|
|
12
|
+
static { this.componentName = 'copy-button'; }
|
|
13
|
+
static { this.styles = [...PmElement.styles, buttonCss, css `:host { display: inline-flex; vertical-align: middle; } .pm-button { gap: 0.4em; }`]; }
|
|
14
|
+
static { this.properties = {
|
|
15
|
+
value: {},
|
|
16
|
+
for: {},
|
|
17
|
+
label: {},
|
|
18
|
+
copiedLabel: { attribute: 'copied-label' },
|
|
19
|
+
variant: { reflect: true },
|
|
20
|
+
size: { reflect: true },
|
|
21
|
+
showLabel: { type: Boolean, attribute: 'show-label' },
|
|
22
|
+
disabled: { type: Boolean, reflect: true },
|
|
23
|
+
_copied: { state: true },
|
|
24
|
+
}; }
|
|
25
|
+
#timer;
|
|
26
|
+
constructor() {
|
|
27
|
+
super();
|
|
28
|
+
this.label = 'Copy';
|
|
29
|
+
this.copiedLabel = 'Copied';
|
|
30
|
+
this.showLabel = false;
|
|
31
|
+
this._copied = false;
|
|
32
|
+
}
|
|
33
|
+
render() {
|
|
34
|
+
const text = this._copied ? this.copiedLabel : this.label;
|
|
35
|
+
const icon = this._copied
|
|
36
|
+
? 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>`
|
|
37
|
+
: 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"><rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V5.5A1.5 1.5 0 0 0 14.5 4h-9A1.5 1.5 0 0 0 4 5.5v9A1.5 1.5 0 0 0 5.5 16H8"/></svg>`;
|
|
38
|
+
return html `<button type="button" class="pm-button" part="base" data-variant=${this.variant ?? 'ghost'} data-tone=${this._copied ? 'success' : nothing}
|
|
39
|
+
data-size=${this.size ?? 'sm'} ?data-icon=${!this.showLabel} ?disabled=${this.disabled} aria-label=${this.showLabel ? nothing : text} @click=${this.copy}>
|
|
40
|
+
${icon}${this.showLabel ? html `<span>${text}</span>` : nothing}
|
|
41
|
+
</button><span class="pm-visually-hidden" aria-live="polite">${this._copied ? this.copiedLabel : ''}</span>`;
|
|
42
|
+
}
|
|
43
|
+
/** Copy now (also triggered by click). */
|
|
44
|
+
async copy() {
|
|
45
|
+
let text = this.value ?? '';
|
|
46
|
+
if (this.for) {
|
|
47
|
+
const el = this.getRootNode().getElementById?.(this.for) ?? document.getElementById(this.for);
|
|
48
|
+
text = el && 'value' in el && typeof el.value === 'string' ? el.value : (el?.textContent ?? '');
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
await navigator.clipboard.writeText(text);
|
|
52
|
+
this._copied = true;
|
|
53
|
+
this.emit('pm-copy', { value: text });
|
|
54
|
+
clearTimeout(this.#timer);
|
|
55
|
+
this.#timer = setTimeout(() => (this._copied = false), 2000);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
this.emit('pm-error', { error });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
defineElement('pm-copy-button', PmCopyButton);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type PropertyDeclarations } from 'lit';
|
|
2
|
+
import { PmElement } from '../base.js';
|
|
3
|
+
/**
|
|
4
|
+
* Countdown to a date and time (a launch, an offer ending, a session expiring).
|
|
5
|
+
*
|
|
6
|
+
* Shows days, hours, minutes and seconds, updates every second and fires `pm-complete` at zero. Screen readers get
|
|
7
|
+
* the remaining time once a minute, not every second.
|
|
8
|
+
* @tag pm-countdown
|
|
9
|
+
* @slot - Shown instead of the numbers once the time has passed.
|
|
10
|
+
* @fires pm-complete - The target time was reached.
|
|
11
|
+
* @example <pm-countdown to="2027-01-01T00:00:00Z" label="Launch in"></pm-countdown>
|
|
12
|
+
*/
|
|
13
|
+
export declare class PmCountdown extends PmElement {
|
|
14
|
+
#private;
|
|
15
|
+
static componentName: string;
|
|
16
|
+
static styles: import("lit").CSSResult[];
|
|
17
|
+
static properties: PropertyDeclarations;
|
|
18
|
+
/** Target time: an ISO date-time ("2027-01-01T00:00:00Z"), or a number of seconds from now ("90"). */
|
|
19
|
+
to?: string;
|
|
20
|
+
/** Accessible name, e.g. "Offer ends in". */
|
|
21
|
+
label?: string;
|
|
22
|
+
size?: 'sm' | 'md';
|
|
23
|
+
/** blocks (default: big numbers with labels) or inline (fits in a sentence: "2d 4h 10m 5s"). */
|
|
24
|
+
variant?: 'blocks' | 'inline';
|
|
25
|
+
/** Which units to show, largest first (default "d,h,m,s"). */
|
|
26
|
+
units?: string;
|
|
27
|
+
_now: number;
|
|
28
|
+
constructor();
|
|
29
|
+
protected willUpdate(changed: Map<PropertyKey, unknown>): void;
|
|
30
|
+
connectedCallback(): void;
|
|
31
|
+
disconnectedCallback(): void;
|
|
32
|
+
/** Milliseconds left (0 when passed or the date is invalid). */
|
|
33
|
+
get remaining(): number;
|
|
34
|
+
protected updated(): void;
|
|
35
|
+
render(): import("lit").TemplateResult<1>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { css, html, nothing } from 'lit';
|
|
2
|
+
import { PmElement, defineElement } from '../base.js';
|
|
3
|
+
import helpersCss from '../styles/helpers.js';
|
|
4
|
+
/**
|
|
5
|
+
* Countdown to a date and time (a launch, an offer ending, a session expiring).
|
|
6
|
+
*
|
|
7
|
+
* Shows days, hours, minutes and seconds, updates every second and fires `pm-complete` at zero. Screen readers get
|
|
8
|
+
* the remaining time once a minute, not every second.
|
|
9
|
+
* @tag pm-countdown
|
|
10
|
+
* @slot - Shown instead of the numbers once the time has passed.
|
|
11
|
+
* @fires pm-complete - The target time was reached.
|
|
12
|
+
* @example <pm-countdown to="2027-01-01T00:00:00Z" label="Launch in"></pm-countdown>
|
|
13
|
+
*/
|
|
14
|
+
export class PmCountdown extends PmElement {
|
|
15
|
+
static { this.componentName = 'countdown'; }
|
|
16
|
+
static { this.styles = [...PmElement.styles, helpersCss, css `:host { display: inline-block; } :host([hidden]) { display: none; }`]; }
|
|
17
|
+
static { this.properties = { to: {}, label: {}, size: { reflect: true }, variant: { reflect: true }, units: {}, _now: { state: true } }; }
|
|
18
|
+
#timer = 0;
|
|
19
|
+
#target = 0;
|
|
20
|
+
#done = false;
|
|
21
|
+
constructor() { super(); this._now = Date.now(); }
|
|
22
|
+
willUpdate(changed) {
|
|
23
|
+
super.willUpdate(changed);
|
|
24
|
+
if (changed.has('to')) {
|
|
25
|
+
const t = this.to ?? '';
|
|
26
|
+
this.#target = /^\d+(\.\d+)?$/.test(t.trim()) ? Date.now() + Number(t) * 1000 : Date.parse(t);
|
|
27
|
+
this.#done = false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
connectedCallback() { super.connectedCallback(); this.#timer = window.setInterval(() => { this._now = Date.now(); }, 1000); }
|
|
31
|
+
disconnectedCallback() { super.disconnectedCallback(); clearInterval(this.#timer); }
|
|
32
|
+
/** Milliseconds left (0 when passed or the date is invalid). */
|
|
33
|
+
get remaining() { return Number.isFinite(this.#target) ? Math.max(0, this.#target - this._now) : 0; }
|
|
34
|
+
updated() {
|
|
35
|
+
if (!this.#done && Number.isFinite(this.#target) && this.remaining === 0) {
|
|
36
|
+
this.#done = true;
|
|
37
|
+
this.emit('pm-complete');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
render() {
|
|
41
|
+
let s = Math.ceil(this.remaining / 1000);
|
|
42
|
+
const want = (this.units ?? 'd,h,m,s').split(',').map((u) => u.trim());
|
|
43
|
+
const SIZE = { d: [86400, 'days', 'd'], h: [3600, 'hours', 'h'], m: [60, 'min', 'm'], s: [1, 'sec', 's'] };
|
|
44
|
+
const parts = want.filter((u) => SIZE[u]).map((u) => { const [n, long, short] = SIZE[u]; const v = Math.floor(s / n); s -= v * n; return { v, long, short }; });
|
|
45
|
+
const inline = this.variant === 'inline';
|
|
46
|
+
if (this.remaining === 0 && this.#done)
|
|
47
|
+
return html `<span part="base"><slot>${parts.map((p) => `0${p.short}`).join(' ')}</slot></span>`;
|
|
48
|
+
// Announced text changes once a minute.
|
|
49
|
+
const mins = Math.ceil(this.remaining / 60000);
|
|
50
|
+
const spoken = `${this.label ? `${this.label} ` : ''}${mins >= 1440 ? `${Math.floor(mins / 1440)} days ${Math.floor((mins % 1440) / 60)} hours` : mins >= 60 ? `${Math.floor(mins / 60)} hours ${mins % 60} minutes` : `${mins} minutes`}`;
|
|
51
|
+
return html `<span class="pm-countdown" part="base" data-size=${this.size ?? nothing} data-variant=${this.variant ?? nothing} role="timer" aria-label=${spoken}>
|
|
52
|
+
${parts.map((p) => html `<span class="pm-countdown-unit" aria-hidden="true"><span class="pm-countdown-value">${inline ? p.v : String(p.v).padStart(2, '0')}</span><span class="pm-countdown-label">${inline ? p.short : p.long}</span></span>`)}
|
|
53
|
+
</span>`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
defineElement('pm-countdown', PmCountdown);
|