@redvars/peacock 4.5.1 → 4.6.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/dist/{BaseField-BbOj212R.js → BaseField-DOCmylob.js} +16 -3
- package/dist/BaseField-DOCmylob.js.map +1 -0
- package/dist/accordion.js +1 -1
- package/dist/activation-gate-D4IpmwyD.js +23 -0
- package/dist/activation-gate-D4IpmwyD.js.map +1 -0
- package/dist/avatar-group.js +2 -1
- package/dist/avatar-group.js.map +1 -1
- package/dist/avatar.js +3 -0
- package/dist/avatar.js.map +1 -1
- package/dist/{base-button-BJMGMcMv.js → base-button-9tGu5RSC.js} +30 -10
- package/dist/base-button-9tGu5RSC.js.map +1 -0
- package/dist/button-group.js +5 -3
- package/dist/button-group.js.map +1 -1
- package/dist/button.js +12 -16
- package/dist/button.js.map +1 -1
- package/dist/calendar-column-view.js +4 -4
- package/dist/{calendar-drag-IXMPZrTN.js → calendar-drag-DdJwSpEz.js} +2 -2
- package/dist/{calendar-drag-IXMPZrTN.js.map → calendar-drag-DdJwSpEz.js.map} +1 -1
- package/dist/{calendar-event-BQChlMXy.js → calendar-event-BvbYZHx1.js} +2 -2
- package/dist/{calendar-event-BQChlMXy.js.map → calendar-event-BvbYZHx1.js.map} +1 -1
- package/dist/calendar-month-view.js +65 -14
- package/dist/calendar-month-view.js.map +1 -1
- package/dist/calendar-timeline.js +9 -4
- package/dist/calendar-timeline.js.map +1 -1
- package/dist/calendar.js +21 -6
- package/dist/calendar.js.map +1 -1
- package/dist/card.js +29 -16
- package/dist/card.js.map +1 -1
- package/dist/chart-doughnut.js +3 -1
- package/dist/chart-doughnut.js.map +1 -1
- package/dist/chart-pie.js +9 -1
- package/dist/chart-pie.js.map +1 -1
- package/dist/chip-set.js +1 -1
- package/dist/chip.js +10 -19
- package/dist/chip.js.map +1 -1
- package/dist/code-editor.js +56 -5
- package/dist/code-editor.js.map +1 -1
- package/dist/color-field.js +4 -1
- package/dist/color-field.js.map +1 -1
- package/dist/currency-field.js +4 -1
- package/dist/currency-field.js.map +1 -1
- package/dist/custom-elements-jsdocs.json +29 -23
- package/dist/custom-elements.json +758 -93
- package/dist/{datasource-OwvH7SVq.js → datasource-DxEA6jHW.js} +6 -4
- package/dist/datasource-DxEA6jHW.js.map +1 -0
- package/dist/{date-cfPx3alz.js → date-DPQOY8Zd.js} +2 -2
- package/dist/{date-cfPx3alz.js.map → date-DPQOY8Zd.js.map} +1 -1
- package/dist/date-field.js +8 -5
- package/dist/date-field.js.map +1 -1
- package/dist/date-picker.js +2 -2
- package/dist/date-range-field.js +8 -5
- package/dist/date-range-field.js.map +1 -1
- package/dist/datetime-field.js +7 -4
- package/dist/datetime-field.js.map +1 -1
- package/dist/diagram-controls.js +1 -1
- package/dist/{throttle-SQ9t-HL7.js → dispatch-event-utils-DfgqVBK8.js} +2 -24
- package/dist/dispatch-event-utils-DfgqVBK8.js.map +1 -0
- package/dist/{form-submitter-beG1CXAn.js → dispatch-hooks-J_godlHh.js} +2 -87
- package/dist/dispatch-hooks-J_godlHh.js.map +1 -0
- package/dist/dropdown-button.js +11 -7
- package/dist/dropdown-button.js.map +1 -1
- package/dist/{event-manager-KGMT2x2Q.js → event-manager-DYfeN3VE.js} +3 -3
- package/dist/{event-manager-KGMT2x2Q.js.map → event-manager-DYfeN3VE.js.map} +1 -1
- package/dist/fab.js +15 -19
- package/dist/fab.js.map +1 -1
- package/dist/filter-bar.js +3 -3
- package/dist/form-submission-h40coUI8.js +13 -0
- package/dist/form-submission-h40coUI8.js.map +1 -0
- package/dist/form-submitter-BIFqZ0wq.js +88 -0
- package/dist/form-submitter-BIFqZ0wq.js.map +1 -0
- package/dist/form.js +1 -9
- package/dist/form.js.map +1 -1
- package/dist/icon-button.js +15 -19
- package/dist/icon-button.js.map +1 -1
- package/dist/icon.js +1 -1
- package/dist/implicit-submission-ChLdbxaR.js +100 -0
- package/dist/implicit-submission-ChLdbxaR.js.map +1 -0
- package/dist/index.js +20 -15
- package/dist/index.js.map +1 -1
- package/dist/{indicator-navigation-item-CmyhsOmy.js → indicator-navigation-item-BsQE3kUr.js} +2 -2
- package/dist/{indicator-navigation-item-CmyhsOmy.js.map → indicator-navigation-item-BsQE3kUr.js.map} +1 -1
- package/dist/input.js +13 -4
- package/dist/input.js.map +1 -1
- package/dist/json-viewer.js +1 -1
- package/dist/kanban.js +8 -7
- package/dist/kanban.js.map +1 -1
- package/dist/link-button.js +13 -14
- package/dist/link-button.js.map +1 -1
- package/dist/list-item.js +52 -16
- package/dist/list-item.js.map +1 -1
- package/dist/list.js +5 -3
- package/dist/list.js.map +1 -1
- package/dist/listbox.js +1 -1
- package/dist/loader-full.js +17 -12
- package/dist/loader-full.js.map +1 -1
- package/dist/loader.js +17 -12
- package/dist/loader.js.map +1 -1
- package/dist/locale-DEsJZCQG.js.map +1 -1
- package/dist/mention-input.js +8 -1
- package/dist/mention-input.js.map +1 -1
- package/dist/menu-item.js +12 -12
- package/dist/menu-item.js.map +1 -1
- package/dist/menu.js +7 -3
- package/dist/menu.js.map +1 -1
- package/dist/navigation-bar-item.js +4 -3
- package/dist/navigation-bar-item.js.map +1 -1
- package/dist/navigation-bar.js +4 -3
- package/dist/navigation-bar.js.map +1 -1
- package/dist/navigation-drawer-item.js +3 -2
- package/dist/navigation-drawer-item.js.map +1 -1
- package/dist/navigation-drawer.js +3 -2
- package/dist/navigation-drawer.js.map +1 -1
- package/dist/{navigation-item-base-kUrvJgCx.js → navigation-item-base-5ljwtLG_.js} +3 -2
- package/dist/{navigation-item-base-kUrvJgCx.js.map → navigation-item-base-5ljwtLG_.js.map} +1 -1
- package/dist/navigation-rail-item.js +4 -3
- package/dist/navigation-rail-item.js.map +1 -1
- package/dist/navigation-rail.js +6 -5
- package/dist/navigation-rail.js.map +1 -1
- package/dist/number-field.js +4 -1
- package/dist/number-field.js.map +1 -1
- package/dist/option.js +3 -2
- package/dist/option.js.map +1 -1
- package/dist/password-field.js +11 -4
- package/dist/password-field.js.map +1 -1
- package/dist/phone-field.js +4 -1
- package/dist/phone-field.js.map +1 -1
- package/dist/{picker-field-DmXYPvbF.js → picker-field-BiM3iXAQ.js} +2 -2
- package/dist/{picker-field-DmXYPvbF.js.map → picker-field-BiM3iXAQ.js.map} +1 -1
- package/dist/pin-input.js +4 -1
- package/dist/pin-input.js.map +1 -1
- package/dist/{range-utils-DZe96GnO.js → range-utils-vgG1SJ2o.js} +2 -2
- package/dist/{range-utils-DZe96GnO.js.map → range-utils-vgG1SJ2o.js.map} +1 -1
- package/dist/{roving-focus-controller-fm95JkZJ.js → roving-focus-controller-YtNbxH95.js} +30 -7
- package/dist/roving-focus-controller-YtNbxH95.js.map +1 -0
- package/dist/search.js +12 -6
- package/dist/search.js.map +1 -1
- package/dist/select.js +11 -3
- package/dist/select.js.map +1 -1
- package/dist/shipped-icons.js +1 -1
- package/dist/split-button.js +9 -1
- package/dist/split-button.js.map +1 -1
- package/dist/src/__internal/controllers/roving-focus-controller.d.ts +14 -2
- package/dist/src/__internal/implicit-submission.d.ts +25 -0
- package/dist/src/__internal/utils/activation-gate.d.ts +8 -0
- package/dist/src/__internal/utils/form-submission.d.ts +2 -0
- package/dist/src/avatar-group/avatar-group.d.ts +1 -1
- package/dist/src/button/base-button/base-button.d.ts +1 -1
- package/dist/src/button/button/button.d.ts +2 -1
- package/dist/src/button/fab/fab.d.ts +2 -1
- package/dist/src/button/icon-button/icon-button.d.ts +2 -1
- package/dist/src/button/link-button/link-button.d.ts +5 -1
- package/dist/src/calendar/calendar-month-view.d.ts +5 -1
- package/dist/src/calendar/calendar.d.ts +6 -1
- package/dist/src/card/card.d.ts +19 -9
- package/dist/src/chart-pie/chart-pie.d.ts +5 -0
- package/dist/src/chip/chip/chip.d.ts +4 -4
- package/dist/src/code-editor/code-editor.d.ts +3 -3
- package/dist/src/dropdown-button/dropdown-button.d.ts +5 -1
- package/dist/src/input/BaseField.d.ts +7 -0
- package/dist/src/input/input.d.ts +5 -1
- package/dist/src/list/list-item.d.ts +2 -1
- package/dist/src/mention-input/mention-input.d.ts +2 -0
- package/dist/src/menu/menu-item/menu-item.d.ts +6 -4
- package/dist/src/password-field/password-field.d.ts +3 -1
- package/dist/src/search/search.d.ts +1 -0
- package/dist/src/select/select.d.ts +2 -0
- package/dist/src/split-button/split-button.d.ts +6 -1
- package/dist/src/stepper/step.d.ts +3 -1
- package/dist/src/tabs/tab.d.ts +12 -2
- package/dist/src/textarea/textarea.d.ts +2 -0
- package/dist/src/toolbar/toolbar.d.ts +13 -2
- package/dist/step.js +7 -1
- package/dist/step.js.map +1 -1
- package/dist/stepper.js +10 -1
- package/dist/stepper.js.map +1 -1
- package/dist/sub-menu.js +3 -2
- package/dist/sub-menu.js.map +1 -1
- package/dist/svg.js +1 -1
- package/dist/tab-group.js +3 -2
- package/dist/tab-group.js.map +1 -1
- package/dist/tab.js +17 -12
- package/dist/tab.js.map +1 -1
- package/dist/tabs.js +3 -2
- package/dist/tabs.js.map +1 -1
- package/dist/test/accessible-name.test.d.ts +1 -0
- package/dist/test/helpers/count-activations.d.ts +9 -0
- package/dist/test/navigation-rail.test.d.ts +1 -0
- package/dist/test/throttle-delay.test.d.ts +1 -0
- package/dist/textarea.js +8 -1
- package/dist/textarea.js.map +1 -1
- package/dist/throttle-C7ZAPqtu.js +24 -0
- package/dist/throttle-C7ZAPqtu.js.map +1 -0
- package/dist/time-field.js +7 -4
- package/dist/time-field.js.map +1 -1
- package/dist/time-picker.js +2 -2
- package/dist/toolbar.js +25 -8
- package/dist/toolbar.js.map +1 -1
- package/dist/tree.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/url-field.js +4 -1
- package/dist/url-field.js.map +1 -1
- package/dist/{utils-DcvLgexs.js → utils-CQzycr9J.js} +2 -1
- package/dist/utils-CQzycr9J.js.map +1 -0
- package/package.json +2 -1
- package/src/__internal/controllers/roving-focus-controller.ts +35 -7
- package/src/__internal/i18n/locale.ts +1 -1
- package/src/__internal/implicit-submission.ts +102 -0
- package/src/__internal/utils/activation-gate.ts +20 -0
- package/src/__internal/utils/cache-fetch.ts +6 -3
- package/src/__internal/utils/form-submission.ts +11 -0
- package/src/avatar/avatar.scss +5 -0
- package/src/avatar-group/avatar-group.scss +3 -0
- package/src/avatar-group/avatar-group.ts +1 -1
- package/src/button/base-button/base-button.ts +41 -16
- package/src/button/button/button.ts +5 -16
- package/src/button/fab/fab.ts +7 -18
- package/src/button/icon-button/icon-button.ts +7 -18
- package/src/button/link-button/link-button.ts +8 -15
- package/src/calendar/_calendar-header.scss +7 -1
- package/src/calendar/calendar-month-view.scss +36 -2
- package/src/calendar/calendar-month-view.ts +33 -8
- package/src/calendar/calendar.ts +17 -2
- package/src/calendar/utils.ts +1 -0
- package/src/card/card.ts +27 -16
- package/src/chart-doughnut/chart-doughnut.ts +6 -1
- package/src/chart-pie/chart-pie.ts +10 -1
- package/src/chip/chip/chip.ts +6 -26
- package/src/code-editor/code-editor.scss +70 -1
- package/src/code-editor/code-editor.ts +12 -4
- package/src/dropdown-button/dropdown-button.ts +7 -2
- package/src/form/form.ts +1 -9
- package/src/input/BaseField.ts +17 -0
- package/src/input/input.ts +13 -1
- package/src/kanban/kanban.scss +11 -7
- package/src/list/list-item.scss +5 -0
- package/src/list/list-item.ts +44 -16
- package/src/mention-input/mention-input.ts +5 -0
- package/src/menu/menu/menu.ts +4 -1
- package/src/menu/menu-item/menu-item.ts +9 -14
- package/src/navigation-rail/navigation-rail.scss +3 -2
- package/src/password-field/password-field.ts +11 -1
- package/src/search/search.ts +9 -0
- package/src/select/select.ts +5 -0
- package/src/split-button/split-button.ts +10 -1
- package/src/stepper/step.ts +7 -1
- package/src/stepper/stepper.scss +13 -0
- package/src/tabs/tab.ts +21 -17
- package/src/textarea/textarea.ts +5 -0
- package/src/toolbar/toolbar.ts +26 -7
- package/dist/BaseField-BbOj212R.js.map +0 -1
- package/dist/base-button-BJMGMcMv.js.map +0 -1
- package/dist/datasource-OwvH7SVq.js.map +0 -1
- package/dist/form-submitter-beG1CXAn.js.map +0 -1
- package/dist/roving-focus-controller-fm95JkZJ.js.map +0 -1
- package/dist/throttle-SQ9t-HL7.js.map +0 -1
- package/dist/utils-DcvLgexs.js.map +0 -1
package/dist/url-field.js
CHANGED
|
@@ -4,12 +4,15 @@ import { r } from './state-BCeDVCI9.js';
|
|
|
4
4
|
import { e } from './query-CHb9Ft_d.js';
|
|
5
5
|
import { s as spread } from './spread-B5cgadZl.js';
|
|
6
6
|
import { I as I18nController } from './i18n-controller-BTeChxE0.js';
|
|
7
|
-
import { B as BaseField } from './BaseField-
|
|
7
|
+
import { B as BaseField } from './BaseField-DOCmylob.js';
|
|
8
8
|
import { n as nativeValidity } from './BaseInput-Cv03GZLu.js';
|
|
9
9
|
import './base-Cl6v8-BZ.js';
|
|
10
10
|
import './directive-ZPhl09Yt.js';
|
|
11
11
|
import './strings-DeiQWMd9.js';
|
|
12
12
|
import './locale-DEsJZCQG.js';
|
|
13
|
+
import './implicit-submission-ChLdbxaR.js';
|
|
14
|
+
import './dispatch-hooks-J_godlHh.js';
|
|
15
|
+
import './form-submission-h40coUI8.js';
|
|
13
16
|
import './element-internals-QME0yEz4.js';
|
|
14
17
|
import './form-associated-BHbg72PG.js';
|
|
15
18
|
import './suggestion-controller-7YRQqkaI.js';
|
package/dist/url-field.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url-field.js","sources":["../../src/url-field/url-field.ts"],"sourcesContent":["import { html, nothing } from 'lit';\nimport { property, query, state } from 'lit/decorators.js';\n\nimport { spread } from '@/__internal/directive/spread.js';\n\nimport IndividualComponent from '@/IndividualComponent.js';\nimport { I18nController } from '@/__internal/i18n/i18n-controller.js';\nimport BaseField from '../input/BaseField.js';\nimport { type ControlValidity, nativeValidity } from '../input/BaseInput.js';\nimport styles from './url-field.scss';\n\n/**\n * @label URL Field\n * @tag wc-url-field\n * @rawTag url-field\n *\n * @summary A field for a web address that shows it as a link until the person edits it, and checks that it is valid.\n * @overview\n * <p>URL Field wraps an input with URL validation, showing a clickable link preview when not in edit mode.\n * The edit button or focusing the field starts editing; leaving the field with a valid URL ends it,\n * and Escape ends it and restores the URL editing started with. The <code>start</code> and\n * <code>end</code> slots show in both modes.</p>\n * <p>It is form-associated: the URL is submitted under its <code>name</code>, and\n * <code>required</code> or an invalid URL blocks form submission.</p>\n *\n * @fires {CustomEvent<{value: string}>} input - Dispatched as the user types.\n * @fires {CustomEvent<{value: string}>} change - Dispatched when the value is committed.\n *\n * @example\n * ```html\n * <wc-url-field label=\"Website\" value=\"https://example.com\"></wc-url-field>\n * ```\n * @tags form\n */\n@IndividualComponent\nexport class UrlField extends BaseField {\n // ── Static ───────────────────────────────────────────────────────────────\n\n static styles = [styles];\n\n // ── Properties ───────────────────────────────────────────────────────────\n\n @property({ type: String })\n value: string = '';\n\n // ── State ────────────────────────────────────────────────────────────────\n\n @state()\n private isValid: boolean = true;\n\n /** Whether the input shows instead of the link. Starts when the person clicks edit or focuses. */\n @state()\n private isEditing = false;\n\n // ── Queries ──────────────────────────────────────────────────────────────\n\n @query('.url-input')\n private inputElement?: HTMLInputElement;\n\n // ── Private fields ───────────────────────────────────────────────────────\n\n /** The value when editing started; Escape restores it. */\n private valueBeforeEditing = '';\n\n /** Peacock's strings for the invalid URL message. */\n private readonly i18n = new I18nController(this);\n\n // ── Public methods ───────────────────────────────────────────────────────\n\n override focus() {\n if (!this.isEditing && !this.disabled && !this.readonly) {\n this.startEditing();\n return;\n }\n this.inputElement?.focus();\n }\n\n override blur() {\n this.inputElement?.blur();\n }\n\n // ── Protected methods ────────────────────────────────────────────────────\n\n protected override computeValidity(): ControlValidity {\n if (!this.isEditing || !this.inputElement) return super.computeValidity();\n const validity = nativeValidity(this.inputElement);\n // Show Peacock's (translatable) message for a malformed URL instead of the browser's.\n if (validity.flags.typeMismatch) {\n return { ...validity, message: this.i18n.t('invalidUrl') };\n }\n return validity;\n }\n\n protected override validityAnchor() {\n // `@query` gives null while the input is not rendered (display mode).\n return this.inputElement ?? undefined;\n }\n\n // ── Private methods ──────────────────────────────────────────────────────\n\n private async startEditing() {\n if (this.disabled || this.readonly) return;\n if (!this.isEditing) this.valueBeforeEditing = this.value;\n this.isEditing = true;\n await this.updateComplete;\n this.inputElement?.focus();\n }\n\n /** Leaves editing, restores the value editing started with and keeps focus on the field. */\n private async cancelEditing() {\n const changed = this.value !== this.valueBeforeEditing;\n this.value = this.valueBeforeEditing;\n this.isValid = true;\n this.isEditing = false;\n if (changed) {\n this.emitValue('input');\n this.emitValue('change');\n }\n await this.updateComplete;\n this.renderRoot.querySelector<HTMLElement>('.edit-button')?.focus();\n }\n\n private closeEditing() {\n this.isValid = this.validateUrl(this.value);\n if (this.isValid) {\n this.isEditing = false;\n }\n }\n\n private validateUrl(url: string): boolean {\n if (!url) return true;\n\n try {\n new URL(url);\n return true;\n } catch {\n return false;\n }\n }\n\n private handleInput(event: Event) {\n // Replace the native event, which escapes the shadow root, with the field's own.\n event.stopPropagation();\n this.value = (event.target as HTMLInputElement).value;\n this.isValid = true;\n this.emitValue('input');\n }\n\n private handleChange(event: Event) {\n event.stopPropagation();\n this.emitValue('change');\n }\n\n private handleBlur(event: FocusEvent) {\n this.handleFieldFocus(event);\n // Escape already left editing; the blur that follows must not report on the old text.\n if (!this.isEditing) return;\n this.closeEditing();\n }\n\n private handleKeyDown(event: KeyboardEvent) {\n if (event.key !== 'Escape') return;\n // Keep Escape from also closing a dialog the field sits in.\n event.stopPropagation();\n this.cancelEditing();\n }\n\n // ── Render helpers ───────────────────────────────────────────────────────\n\n private renderDisplayValue() {\n if (!this.value) {\n return html`<span class=\"url-placeholder\">${this.placeholder}</span>`;\n }\n\n return html`\n <a\n class=\"url-link\"\n href=${this.value}\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n ${this.value}\n </a>\n `;\n }\n\n private renderEditButton() {\n if (this.isEditing || this.disabled || this.readonly) return nothing;\n return html`\n <wc-icon-button\n class=\"edit-button\"\n slot=\"field-end\"\n shape=\"round\"\n variant=\"text\"\n aria-label=${this.i18n.t('editUrl')}\n @click=${(event: MouseEvent) => {\n event.stopPropagation();\n this.startEditing();\n }}\n >\n <wc-icon name=\"edit\"></wc-icon>\n </wc-icon-button>\n `;\n }\n\n // ── Render ───────────────────────────────────────────────────────────────\n\n render() {\n const controlError = this.isValid ? '' : this.i18n.t('invalidUrl');\n\n const control = this.isEditing\n ? html`<input\n class=\"url-input\"\n type=\"url\"\n placeholder=${this.placeholder}\n .value=${this.value}\n ?readonly=${this.readonly}\n ?required=${this.required}\n ?disabled=${this.disabled}\n @input=${this.handleInput}\n @change=${this.handleChange}\n @keydown=${this.handleKeyDown}\n @focus=${this.handleFieldFocus}\n @blur=${this.handleBlur}\n ${spread(this.fieldAria(controlError))}\n />`\n : html`<div class=\"url-display\">${this.renderDisplayValue()}</div>`;\n\n return this.renderField(control, {\n populated: !!this.value || this.isEditing,\n controlError,\n end: this.renderEditButton(),\n });\n }\n}\n"],"names":["html","nothing","styles","property","state","query"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AAEI,IAAM,QAAQ,GAAd,MAAM,QAAS,SAAQ,SAAS,CAAA;AAAhC,IAAA,WAAA,GAAA;;;;QAQL,IAAA,CAAA,KAAK,GAAW,EAAE;;QAKV,IAAA,CAAA,OAAO,GAAY,IAAI;;QAIvB,IAAA,CAAA,SAAS,GAAG,KAAK;;;QAUjB,IAAA,CAAA,kBAAkB,GAAG,EAAE;;AAGd,QAAA,IAAA,CAAA,IAAI,GAAG,IAAI,cAAc,CAAC,IAAI,CAAC;IAyKlD;;IArKW,KAAK,GAAA;AACZ,QAAA,IAAI,CAAC,IAAI,CAAC,SAAS,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YACvD,IAAI,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AA,EAAA,IAAI,CAAC,UAAU;AACrB,UAAA,EAAA,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;AACrC,UAAA;cACHA,CAAI,CAAA,CAAA,yBAAA,EAA4B,IAAI,CAAC,kBAAkB,EAAE,CAAA,MAAA,CAAQ;AAErE,QAAA,OAAO,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE;YAC/B,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,SAAS;YACzC,YAAY;AACZ,YAAA,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE;AAC7B,SAAA,CAAC;IACJ;;AAnMO,QAAA,CAAA,MAAM,GAAG,CAACE,QAAM,CAAC;AAKxB,UAAA,CAAA;AADC,IAAAC,CAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE;AACP,CAAA,EAAA,QAAA,CAAA,SAAA,EAAA,OAAA,EAAA,MAAA,CAAA;AAKX,UAAA,CAAA;AADP,IAAAC,CAAK;AAC0B,CAAA,EAAA,QAAA,CAAA,SAAA,EAAA,SAAA,EAAA,MAAA,CAAA;AAIxB,UAAA,CAAA;AADP,IAAAA,CAAK;AACoB,CAAA,EAAA,QAAA,CAAA,SAAA,EAAA,WAAA,EAAA,MAAA,CAAA;AAKlB,UAAA,CAAA;IADPC,CAAK,CAAC,YAAY;AACqB,CAAA,EAAA,QAAA,CAAA,SAAA,EAAA,cAAA,EAAA,MAAA,CAAA;AAtB7B,QAAQ,GAAA,UAAA,CAAA;IADpB;AACY,CAAA,EAAA,QAAQ,CAuMpB;;;;"}
|
|
1
|
+
{"version":3,"file":"url-field.js","sources":["../../src/url-field/url-field.ts"],"sourcesContent":["import { html, nothing } from 'lit';\nimport { property, query, state } from 'lit/decorators.js';\n\nimport { spread } from '@/__internal/directive/spread.js';\n\nimport IndividualComponent from '@/IndividualComponent.js';\nimport { I18nController } from '@/__internal/i18n/i18n-controller.js';\nimport BaseField from '../input/BaseField.js';\nimport { type ControlValidity, nativeValidity } from '../input/BaseInput.js';\nimport styles from './url-field.scss';\n\n/**\n * @label URL Field\n * @tag wc-url-field\n * @rawTag url-field\n *\n * @summary A field for a web address that shows it as a link until the person edits it, and checks that it is valid.\n * @overview\n * <p>URL Field wraps an input with URL validation, showing a clickable link preview when not in edit mode.\n * The edit button or focusing the field starts editing; leaving the field with a valid URL ends it,\n * and Escape ends it and restores the URL editing started with. The <code>start</code> and\n * <code>end</code> slots show in both modes.</p>\n * <p>It is form-associated: the URL is submitted under its <code>name</code>, and\n * <code>required</code> or an invalid URL blocks form submission.</p>\n *\n * @fires {CustomEvent<{value: string}>} input - Dispatched as the user types.\n * @fires {CustomEvent<{value: string}>} change - Dispatched when the value is committed.\n *\n * @example\n * ```html\n * <wc-url-field label=\"Website\" value=\"https://example.com\"></wc-url-field>\n * ```\n * @tags form\n */\n@IndividualComponent\nexport class UrlField extends BaseField {\n // ── Static ───────────────────────────────────────────────────────────────\n\n static styles = [styles];\n\n // ── Properties ───────────────────────────────────────────────────────────\n\n @property({ type: String })\n value: string = '';\n\n // ── State ────────────────────────────────────────────────────────────────\n\n @state()\n private isValid: boolean = true;\n\n /** Whether the input shows instead of the link. Starts when the person clicks edit or focuses. */\n @state()\n private isEditing = false;\n\n // ── Queries ──────────────────────────────────────────────────────────────\n\n @query('.url-input')\n private inputElement?: HTMLInputElement;\n\n // ── Private fields ───────────────────────────────────────────────────────\n\n /** The value when editing started; Escape restores it. */\n private valueBeforeEditing = '';\n\n /** Peacock's strings for the invalid URL message. */\n private readonly i18n = new I18nController(this);\n\n // ── Public methods ───────────────────────────────────────────────────────\n\n override focus() {\n if (!this.isEditing && !this.disabled && !this.readonly) {\n this.startEditing();\n return;\n }\n this.inputElement?.focus();\n }\n\n override blur() {\n this.inputElement?.blur();\n }\n\n // ── Protected methods ────────────────────────────────────────────────────\n\n protected override computeValidity(): ControlValidity {\n if (!this.isEditing || !this.inputElement) return super.computeValidity();\n const validity = nativeValidity(this.inputElement);\n // Show Peacock's (translatable) message for a malformed URL instead of the browser's.\n if (validity.flags.typeMismatch) {\n return { ...validity, message: this.i18n.t('invalidUrl') };\n }\n return validity;\n }\n\n protected override validityAnchor() {\n // `@query` gives null while the input is not rendered (display mode).\n return this.inputElement ?? undefined;\n }\n\n // ── Private methods ──────────────────────────────────────────────────────\n\n private async startEditing() {\n if (this.disabled || this.readonly) return;\n if (!this.isEditing) this.valueBeforeEditing = this.value;\n this.isEditing = true;\n await this.updateComplete;\n this.inputElement?.focus();\n }\n\n /** Leaves editing, restores the value editing started with and keeps focus on the field. */\n private async cancelEditing() {\n const changed = this.value !== this.valueBeforeEditing;\n this.value = this.valueBeforeEditing;\n this.isValid = true;\n this.isEditing = false;\n if (changed) {\n this.emitValue('input');\n this.emitValue('change');\n }\n await this.updateComplete;\n this.renderRoot.querySelector<HTMLElement>('.edit-button')?.focus();\n }\n\n private closeEditing() {\n this.isValid = this.validateUrl(this.value);\n if (this.isValid) {\n this.isEditing = false;\n }\n }\n\n private validateUrl(url: string): boolean {\n if (!url) return true;\n\n try {\n new URL(url);\n return true;\n } catch {\n return false;\n }\n }\n\n private handleInput(event: Event) {\n // Replace the native event, which escapes the shadow root, with the field's own.\n event.stopPropagation();\n this.value = (event.target as HTMLInputElement).value;\n this.isValid = true;\n this.emitValue('input');\n }\n\n private handleChange(event: Event) {\n event.stopPropagation();\n this.emitValue('change');\n }\n\n private handleBlur(event: FocusEvent) {\n this.handleFieldFocus(event);\n // Escape already left editing; the blur that follows must not report on the old text.\n if (!this.isEditing) return;\n this.closeEditing();\n }\n\n private handleKeyDown(event: KeyboardEvent) {\n if (event.key !== 'Escape') return;\n // Keep Escape from also closing a dialog the field sits in.\n event.stopPropagation();\n this.cancelEditing();\n }\n\n // ── Render helpers ───────────────────────────────────────────────────────\n\n private renderDisplayValue() {\n if (!this.value) {\n return html`<span class=\"url-placeholder\">${this.placeholder}</span>`;\n }\n\n return html`\n <a\n class=\"url-link\"\n href=${this.value}\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n >\n ${this.value}\n </a>\n `;\n }\n\n private renderEditButton() {\n if (this.isEditing || this.disabled || this.readonly) return nothing;\n return html`\n <wc-icon-button\n class=\"edit-button\"\n slot=\"field-end\"\n shape=\"round\"\n variant=\"text\"\n aria-label=${this.i18n.t('editUrl')}\n @click=${(event: MouseEvent) => {\n event.stopPropagation();\n this.startEditing();\n }}\n >\n <wc-icon name=\"edit\"></wc-icon>\n </wc-icon-button>\n `;\n }\n\n // ── Render ───────────────────────────────────────────────────────────────\n\n render() {\n const controlError = this.isValid ? '' : this.i18n.t('invalidUrl');\n\n const control = this.isEditing\n ? html`<input\n class=\"url-input\"\n type=\"url\"\n placeholder=${this.placeholder}\n .value=${this.value}\n ?readonly=${this.readonly}\n ?required=${this.required}\n ?disabled=${this.disabled}\n @input=${this.handleInput}\n @change=${this.handleChange}\n @keydown=${this.handleKeyDown}\n @focus=${this.handleFieldFocus}\n @blur=${this.handleBlur}\n ${spread(this.fieldAria(controlError))}\n />`\n : html`<div class=\"url-display\">${this.renderDisplayValue()}</div>`;\n\n return this.renderField(control, {\n populated: !!this.value || this.isEditing,\n controlError,\n end: this.renderEditButton(),\n });\n }\n}\n"],"names":["html","nothing","styles","property","state","query"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AAEI,IAAM,QAAQ,GAAd,MAAM,QAAS,SAAQ,SAAS,CAAA;AAAhC,IAAA,WAAA,GAAA;;;;QAQL,IAAA,CAAA,KAAK,GAAW,EAAE;;QAKV,IAAA,CAAA,OAAO,GAAY,IAAI;;QAIvB,IAAA,CAAA,SAAS,GAAG,KAAK;;;QAUjB,IAAA,CAAA,kBAAkB,GAAG,EAAE;;AAGd,QAAA,IAAA,CAAA,IAAI,GAAG,IAAI,cAAc,C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l` options for each display pattern; the output matches the pattern in `en-US`. */
|
|
207
207
|
const DISPLAY_FORMATS = {
|
|
208
208
|
'MMMM d, yyyy': { month: 'long', day: 'numeric', year: 'numeric' },
|
|
209
|
+
'MMMM yyyy': { month: 'long', year: 'numeric' },
|
|
209
210
|
EEEE: { weekday: 'long' },
|
|
210
211
|
E: { weekday: 'short' },
|
|
211
212
|
d: { day: 'numeric' },
|
|
@@ -280,4 +281,4 @@ function getTimePercent(date, forDay) {
|
|
|
280
281
|
}
|
|
281
282
|
|
|
282
283
|
export { atWallMinutes as A, LONG_EVENT_PADDING as L, MINUTE_MS as M, addDays as a, startOfDay as b, addMonths as c, differenceInDays as d, endOfDay as e, formatEventWhen as f, formatEventLabel as g, formatDate as h, isAllDayRange as i, getTimePercent as j, calculateDateRange as k, lastDayOf as l, timeAtFraction as m, nextDay as n, snapTime as o, clamp as p, onActivateKey as q, rangesOverlap as r, startOfWeek as s, toZonedWallClock as t, addHours as u, formatTimeRange as v, calculateMonthRange as w, startOfMonth as x, endOfMonth as y, wallMinutes as z };
|
|
283
|
-
//# sourceMappingURL=utils-
|
|
284
|
+
//# sourceMappingURL=utils-CQzycr9J.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils-CQzycr9J.js","sources":["../../src/calendar/utils.ts"],"sourcesContent":["/**\n * Pure date utility functions (no external dependencies).\n */\n\nimport { resolveLocale } from '@/__internal/i18n/locale.js';\n\nexport function startOfDay(date: Date): Date {\n const d = new Date(date);\n d.setHours(0, 0, 0, 0);\n return d;\n}\n\nexport function endOfDay(date: Date): Date {\n const d = new Date(date);\n d.setHours(23, 59, 59, 999);\n return d;\n}\n\nexport function addDays(date: Date, days: number): Date {\n const d = new Date(date);\n d.setDate(d.getDate() + days);\n return d;\n}\n\nexport function addMonths(date: Date, months: number): Date {\n const d = new Date(date);\n d.setMonth(d.getMonth() + months);\n return d;\n}\n\nexport function addHours(date: Date, hours: number): Date {\n const d = new Date(date);\n d.setHours(d.getHours() + hours);\n return d;\n}\n\nexport function differenceInDays(dateA: Date, dateB: Date): number {\n const a = startOfDay(dateA);\n const b = startOfDay(dateB);\n return Math.round((a.valueOf() - b.valueOf()) / 86400000);\n}\n\nexport function startOfWeek(\n date: Date,\n weekStartsOn: 0 | 1 | 2 | 3 | 4 | 5 | 6 = 1,\n): Date {\n const d = startOfDay(date);\n const day = d.getDay();\n const diff = (day < weekStartsOn ? 7 : 0) + day - weekStartsOn;\n d.setDate(d.getDate() - diff);\n return d;\n}\n\nexport function endOfWeek(\n date: Date,\n weekStartsOn: 0 | 1 | 2 | 3 | 4 | 5 | 6 = 1,\n): Date {\n const d = startOfWeek(date, weekStartsOn);\n d.setDate(d.getDate() + 6);\n return endOfDay(d);\n}\n\nexport function startOfMonth(date: Date): Date {\n const d = new Date(date);\n d.setDate(1);\n d.setHours(0, 0, 0, 0);\n return d;\n}\n\nexport function endOfMonth(date: Date): Date {\n const d = new Date(date);\n d.setMonth(d.getMonth() + 1, 0);\n d.setHours(23, 59, 59, 999);\n return d;\n}\n\nexport function calculateMonthRange(\n date: Date,\n weekStartsOn: 0 | 1 | 2 | 3 | 4 | 5 | 6 = 1,\n) {\n const sd = startOfWeek(startOfMonth(date), weekStartsOn);\n const ed = endOfWeek(endOfMonth(date), weekStartsOn);\n return { startDate: sd, endDate: ed, totalDays: 42 };\n}\n\nexport function calculateWeekRange(\n date: Date,\n weekStartsOn: 0 | 1 | 2 | 3 | 4 | 5 | 6 = 1,\n) {\n const sd = startOfWeek(date, weekStartsOn);\n const ed = endOfWeek(date, weekStartsOn);\n return { startDate: sd, endDate: ed, totalDays: 7 };\n}\n\nexport function calculateDateRange(\n view: string,\n contextDate: Date,\n days: number,\n weekStartsOn: 0 | 1 | 2 | 3 | 4 | 5 | 6 = 1,\n) {\n if (view === 'week') {\n return calculateWeekRange(contextDate, weekStartsOn);\n }\n return {\n startDate: startOfDay(contextDate),\n endDate: endOfDay(addDays(contextDate, days - 1)),\n totalDays: days,\n };\n}\n\nexport const LONG_EVENT_PADDING = 0.25;\n\n/** Milliseconds in a day of 24 hours. */\nexport const DAY_MS = 86400000;\n\n/** Milliseconds in a minute. */\nexport const MINUTE_MS = 60000;\n\nexport function clamp(value: number, min: number, max: number): number {\n return Math.min(max, Math.max(min, value));\n}\n\n/** Whether `date` is at local midnight. */\nexport function isMidnight(date: Date): boolean {\n return (\n date.getHours() === 0 &&\n date.getMinutes() === 0 &&\n date.getSeconds() === 0 &&\n date.getMilliseconds() === 0\n );\n}\n\n/**\n * Whether a range belongs in the all-day row: it lasts a day or more, or runs from one midnight to\n * a later one (a day the clocks go forward on has only 23 hours).\n */\nexport function isAllDayRange(start: Date, end: Date): boolean {\n const length = end.valueOf() - start.valueOf();\n return (\n length >= DAY_MS || (length > 0 && isMidnight(start) && isMidnight(end))\n );\n}\n\n/**\n * The last day a range covers. `end` is exclusive, so a range that ends at midnight ends the day\n * before. Never earlier than the day of `start`.\n */\nexport function lastDayOf(start: Date, end: Date): Date {\n const first = startOfDay(start);\n const last = startOfDay(new Date(end.valueOf() - 1));\n return last.valueOf() < first.valueOf() ? first : last;\n}\n\n/**\n * Whether the range `start`–`end` (end exclusive) shares time with `from`–`to`. A range without\n * length counts when its instant falls inside.\n */\nexport function rangesOverlap(\n start: Date,\n end: Date,\n from: Date,\n to: Date,\n): boolean {\n if (start.valueOf() >= end.valueOf()) {\n return start.valueOf() >= from.valueOf() && start.valueOf() < to.valueOf();\n }\n return start.valueOf() < to.valueOf() && end.valueOf() > from.valueOf();\n}\n\n/**\n * The start of the day after `date`'s. Unlike `addDays(day, 1)`, it lands on midnight even after a\n * day whose midnight the clocks skipped, where `startOfDay` gives 01:00.\n */\nexport function nextDay(date: Date): Date {\n return startOfDay(addDays(startOfDay(date), 1));\n}\n\n/** Minutes in a day on the wall clock, which the day and week views draw. */\nconst DAY_MINUTES = 1440;\n\n/**\n * Wall-clock minutes from the start of `day` to `date`: negative before it, 1440 or more after\n * it. The day and week views draw 24 equal hours, also on the days the clocks change.\n */\nexport function wallMinutes(date: Date, day: Date): number {\n return (\n differenceInDays(date, day) * DAY_MINUTES +\n date.getHours() * 60 +\n date.getMinutes() +\n date.getSeconds() / 60 +\n date.getMilliseconds() / MINUTE_MS\n );\n}\n\n/** The time `minutes` after the start of `day` on the wall clock; 1440 is the next midnight. */\nexport function atWallMinutes(day: Date, minutes: number): Date {\n const date = startOfDay(day);\n date.setHours(0, 0, 0, Math.round(minutes * MINUTE_MS));\n return date;\n}\n\n/** The time at `fraction` (0 to 1) down `day`'s column: the inverse of `getTimePercent`. */\nexport function timeAtFraction(day: Date, fraction: number): Date {\n return atWallMinutes(day, clamp(fraction, 0, 1) * DAY_MINUTES);\n}\n\n/**\n * `date` moved to a multiple of `minutes` on the wall clock after the start of `day` (by default\n * its own day): the nearest one, or the one before or after. The result stays within `day`, whose\n * end is the next midnight.\n */\nexport function snapTime(\n date: Date,\n minutes: number,\n mode: 'round' | 'floor' | 'ceil' = 'round',\n day: Date = date,\n): Date {\n const step = Math.max(1, minutes);\n const steps = Math[mode](wallMinutes(date, day) / step);\n return atWallMinutes(day, clamp(steps * step, 0, DAY_MINUTES));\n}\n\n/** Wall-clock date and time fields; `month` is 1–12. */\nexport type WallClockParts = {\n year: number;\n month: number;\n day: number;\n hour: number;\n minute: number;\n second: number;\n};\n\n/**\n * The wall-clock fields `date` shows in `timeZone` (an IANA name such as `America/New_York`),\n * read from `Intl.DateTimeFormat#formatToParts`. Throws a `RangeError` for an unknown zone.\n */\nexport function zonedParts(date: Date, timeZone: string): WallClockParts {\n const parts = new Intl.DateTimeFormat('en-US', {\n timeZone,\n hourCycle: 'h23',\n year: 'numeric',\n month: 'numeric',\n day: 'numeric',\n hour: 'numeric',\n minute: 'numeric',\n second: 'numeric',\n }).formatToParts(date);\n const field = (type: Intl.DateTimeFormatPartTypes) =>\n Number(parts.find(part => part.type === type)?.value ?? 0);\n return {\n year: field('year'),\n month: field('month'),\n day: field('day'),\n // Some engines print midnight as 24 even with `h23`.\n hour: field('hour') % 24,\n minute: field('minute'),\n second: field('second'),\n };\n}\n\n/**\n * A `Date` whose local fields show the wall-clock time `date` has in `timeZone`, so the views can\n * lay it out with local getters. An empty or unknown zone returns a copy of `date`.\n */\nexport function toZonedWallClock(date: Date, timeZone: string): Date {\n if (!timeZone) return new Date(date);\n try {\n const { year, month, day, hour, minute, second } = zonedParts(\n date,\n timeZone,\n );\n const wall = new Date(\n year,\n month - 1,\n day,\n hour,\n minute,\n second,\n date.getMilliseconds(),\n );\n // Two-digit years would otherwise land in the 1900s.\n wall.setFullYear(year);\n return wall;\n } catch {\n return new Date(date);\n }\n}\n\n/** `Intl` options for each display pattern; the output matches the pattern in `en-US`. */\nconst DISPLAY_FORMATS: Record<string, Intl.DateTimeFormatOptions> = {\n 'MMMM d, yyyy': { month: 'long', day: 'numeric', year: 'numeric' },\n 'MMMM yyyy': { month: 'long', year: 'numeric' },\n EEEE: { weekday: 'long' },\n E: { weekday: 'short' },\n d: { day: 'numeric' },\n dd: { day: '2-digit' },\n 'hh a': { hour: '2-digit' },\n 'hh:mm a': { hour: '2-digit', minute: '2-digit' },\n};\n\n/**\n * Formats a date for display in `locale`. The patterns name the `en-US` output; other locales get\n * their own month and day names, order and clock. `dd-MM-yyyy` is a fixed key, not display text.\n */\nexport function formatDate(\n date: Date,\n pattern: string,\n locale: string = resolveLocale(),\n): string {\n if (pattern === 'dd-MM-yyyy') {\n const day = String(date.getDate()).padStart(2, '0');\n const month = String(date.getMonth() + 1).padStart(2, '0');\n return `${day}-${month}-${date.getFullYear()}`;\n }\n const options = DISPLAY_FORMATS[pattern];\n if (!options) return date.toLocaleDateString(locale);\n return new Intl.DateTimeFormat(locale, options).format(date);\n}\n\n/** `format.formatRange`, or both dates joined by a dash where it is missing or throws. */\nfunction formatRange(\n format: Intl.DateTimeFormat,\n start: Date,\n end: Date,\n): string {\n try {\n return format.formatRange(start, end);\n } catch {\n return `${format.format(start)} – ${format.format(end)}`;\n }\n}\n\n/**\n * When an event happens, for example `Nov 6, 2026, 9:00 – 9:30 AM`. An all-day event shows its\n * days only, through the day before an `end` at midnight.\n */\nexport function formatEventWhen(\n start: Date,\n end: Date,\n locale: string,\n allDay = false,\n): string {\n if (allDay) {\n const format = new Intl.DateTimeFormat(locale, { dateStyle: 'medium' });\n return formatRange(format, start, lastDayOf(start, end));\n }\n const format = new Intl.DateTimeFormat(locale, {\n dateStyle: 'medium',\n timeStyle: 'short',\n });\n return formatRange(format, start, end);\n}\n\n/** The times of a range without its date, for example `9:00 – 10:30 AM`. */\nexport function formatTimeRange(\n start: Date,\n end: Date,\n locale: string,\n): string {\n const format = new Intl.DateTimeFormat(locale, { timeStyle: 'short' });\n return formatRange(format, start, end);\n}\n\n/** Accessible name of an event: its title and when it happens, for example `Standup, Nov 6, 2026, 9:00 – 9:30 AM`. */\nexport function formatEventLabel(\n title: string,\n start: Date,\n end: Date,\n locale: string,\n allDay = false,\n): string {\n const when = formatEventWhen(start, end, locale, allDay);\n return title ? `${title}, ${when}` : when;\n}\n\n/** Runs `action` on Enter or Space, the keys that activate a button. */\nexport function onActivateKey(action: () => void) {\n return (event: KeyboardEvent) => {\n if (event.key !== 'Enter' && event.key !== ' ') return;\n event.preventDefault();\n action();\n };\n}\n\n/**\n * How far down `forDay`'s column (by default `date`'s own day) `date` falls, in percent, on a\n * 24-hour wall clock: a 9:00 event sits at the 9 AM row also on the days the clocks change.\n */\nexport function getTimePercent(date: Date, forDay?: Date): number {\n return clamp((wallMinutes(date, forDay || date) / DAY_MINUTES) * 100, 0, 100);\n}\n"],"names":[],"mappings":";;AAAA;;AAEG;AAIG,SAAU,UAAU,CAAC,IAAU,EAAA;AACnC,IAAA,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC;IACxB,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACtB,IAAA,OAAO,CAAC;AACV;AAEM,SAAU,QAAQ,CAAC,IAAU,EAAA;A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AAS,EAAE,OAAO,EAAE,CAAC;IACtE,OAAO,WAAW,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC;AACxC;AAEA;AACM,SAAU,gBAAgB,CAC9B,KAAa,EACb,KAAW,EACX,GAAS,EACT,MAAc,EACd,MAAM,GAAG,KAAK,EAAA;AAEd,IAAA,MAAM,IAAI,GAAG,eAAe,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC;AACxD,IAAA,OAAO,KAAK,GAAG,CAAA,EAAG,KAAK,CAAA,EAAA,EAAK,IAAI,CAAA,CAAE,GAAG,IAAI;AAC3C;AAEA;AACM,SAAU,aAAa,CAAC,MAAkB,EAAA;IAC9C,OAAO,CAAC,KAAoB,KAAI;QAC9B,IAAI,KAAK,CAAC,GAAG,KAAK,OAAO,IAAI,KAAK,CAAC,GAAG,KAAK,GAAG;YAAE;QAChD,KAAK,CAAC,cAAc,EAAE;AACtB,QAAA,MAAM,EAAE;AACV,IAAA,CAAC;AACH;AAEA;;;AAGG;AACG,SAAU,cAAc,CAAC,IAAU,EAAE,MAAa,EAAA;IACtD,OAAO,KAAK,CAAC,CAAC,WAAW,CAAC,IAAI,EAAE,MAAM,IAAI,IAAI,CAAC,GAAG,WAAW,IAAI,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC;AAC/E;;;;"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@redvars/peacock",
|
|
3
3
|
"description": "The foundation for beautiful user interfaces",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
|
-
"version": "4.
|
|
5
|
+
"version": "4.6.1",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"module": "dist/index.js",
|
|
@@ -106,6 +106,7 @@
|
|
|
106
106
|
"@wc-toolkit/jsdoc-tags": "^1.1.0",
|
|
107
107
|
"@web/dev-server": "^0.4.6",
|
|
108
108
|
"@web/test-runner": "^0.18.2",
|
|
109
|
+
"@web/test-runner-commands": "^0.9.0",
|
|
109
110
|
"cem-plugin-expanded-types": "^1.4.0",
|
|
110
111
|
"concurrently": "^8.2.2",
|
|
111
112
|
"eslint": "^8.57.0",
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
2
|
|
|
3
3
|
export interface RovingFocusOptions {
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* The items, in order. Disabled or hidden ones are skipped automatically; include them, so that
|
|
6
|
+
* when the item holding the tab stop becomes disabled the next item takes the stop over.
|
|
7
|
+
*/
|
|
5
8
|
items: () => HTMLElement[];
|
|
6
9
|
/** `horizontal` uses Left/Right (mirrored in right-to-left), `vertical` Up/Down, `both` all four. */
|
|
7
10
|
orientation?: () => 'horizontal' | 'vertical' | 'both';
|
|
@@ -80,14 +83,23 @@ export class RovingFocusController implements ReactiveController {
|
|
|
80
83
|
return this.active;
|
|
81
84
|
}
|
|
82
85
|
|
|
83
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* Re-applies the tab stops; call after the items change, including when one becomes disabled
|
|
88
|
+
* or hidden. An item that can no longer take focus hands the tab stop to the focusable item
|
|
89
|
+
* after it (else the one before it), unless `initialItem` picks another.
|
|
90
|
+
*/
|
|
84
91
|
sync() {
|
|
85
|
-
const
|
|
92
|
+
const all = this.options.items();
|
|
93
|
+
const items = this.focusableItems(all);
|
|
86
94
|
if (!this.active || !items.includes(this.active)) {
|
|
87
|
-
this.active =
|
|
95
|
+
this.active =
|
|
96
|
+
this.options.initialItem?.(items) ??
|
|
97
|
+
this.nearestFocusable(all, items) ??
|
|
98
|
+
items[0] ??
|
|
99
|
+
null;
|
|
88
100
|
}
|
|
89
101
|
if (!(this.options.manageTabIndex?.() ?? true)) return;
|
|
90
|
-
for (const item of
|
|
102
|
+
for (const item of all) {
|
|
91
103
|
const stop = item === this.active;
|
|
92
104
|
if (item.tabIndex !== (stop ? 0 : -1)) item.tabIndex = stop ? 0 : -1;
|
|
93
105
|
}
|
|
@@ -131,9 +143,25 @@ export class RovingFocusController implements ReactiveController {
|
|
|
131
143
|
|
|
132
144
|
// ── Private ──────────────────────────────────────────────────────────────
|
|
133
145
|
|
|
134
|
-
private focusableItems() {
|
|
146
|
+
private focusableItems(all = this.options.items()) {
|
|
135
147
|
const focusable = this.options.isFocusable ?? defaultFocusable;
|
|
136
|
-
return
|
|
148
|
+
return all.filter(item => focusable(item));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* For an active item that is still in the set but can no longer take focus: the first focusable
|
|
153
|
+
* item after it, else the nearest one before it.
|
|
154
|
+
*/
|
|
155
|
+
private nearestFocusable(all: HTMLElement[], focusable: HTMLElement[]) {
|
|
156
|
+
const index = this.active ? all.indexOf(this.active) : -1;
|
|
157
|
+
if (index < 0) return undefined;
|
|
158
|
+
return (
|
|
159
|
+
all.slice(index + 1).find(item => focusable.includes(item)) ??
|
|
160
|
+
all
|
|
161
|
+
.slice(0, index)
|
|
162
|
+
.reverse()
|
|
163
|
+
.find(item => focusable.includes(item))
|
|
164
|
+
);
|
|
137
165
|
}
|
|
138
166
|
|
|
139
167
|
private itemFromEvent(event: Event): HTMLElement | null {
|
|
@@ -21,7 +21,7 @@ function closestLang(element: Element): string {
|
|
|
21
21
|
if (node.parentElement) {
|
|
22
22
|
node = node.parentElement;
|
|
23
23
|
} else {
|
|
24
|
-
const root = node.getRootNode();
|
|
24
|
+
const root: Node = node.getRootNode();
|
|
25
25
|
node = root instanceof ShadowRoot ? root.host : null;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { afterDispatch, setupDispatchHooks } from './events/dispatch-hooks.js';
|
|
2
|
+
import { isSubmitButton } from './utils/form-submission.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Native input types that "block implicit submission" in the HTML standard: the single-line
|
|
6
|
+
* fields in which Enter submits the form.
|
|
7
|
+
*/
|
|
8
|
+
const TEXT_INPUT_TYPES = new Set([
|
|
9
|
+
'text',
|
|
10
|
+
'search',
|
|
11
|
+
'url',
|
|
12
|
+
'tel',
|
|
13
|
+
'email',
|
|
14
|
+
'password',
|
|
15
|
+
'date',
|
|
16
|
+
'month',
|
|
17
|
+
'week',
|
|
18
|
+
'time',
|
|
19
|
+
'datetime-local',
|
|
20
|
+
'number',
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Peacock's single-line text fields, registered with `registerTextField()`. They count as text
|
|
25
|
+
* fields too, whether or not Enter in them submits the form.
|
|
26
|
+
*/
|
|
27
|
+
const textFields = new WeakSet<Element>();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Counts a form-associated Peacock field as a single-line text field of its form, like a native
|
|
31
|
+
* text input. A form without a submit button is submitted on Enter only while it has one such
|
|
32
|
+
* field, so a field that leaves this out lets Enter in another field submit the form.
|
|
33
|
+
*
|
|
34
|
+
* `setupImplicitSubmission()` registers its field itself.
|
|
35
|
+
*/
|
|
36
|
+
export function registerTextField(host: Element) {
|
|
37
|
+
textFields.add(host);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function blocksImplicitSubmission(el: Element) {
|
|
41
|
+
if (el instanceof HTMLInputElement) return TEXT_INPUT_TYPES.has(el.type);
|
|
42
|
+
return textFields.has(el);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Submits `form` the way the browser does when Enter is pressed in one of its text inputs. The
|
|
47
|
+
* default button (the form's first submit button) is clicked, so its own click handling runs and
|
|
48
|
+
* it is the `submitter`; when it is disabled, nothing is submitted. Without a submit button, the
|
|
49
|
+
* form is submitted only when it has a single text field.
|
|
50
|
+
*/
|
|
51
|
+
function submitImplicitly(form: HTMLFormElement) {
|
|
52
|
+
const controls = Array.from(form.elements);
|
|
53
|
+
const defaultButton = controls.find(isSubmitButton);
|
|
54
|
+
if (defaultButton instanceof HTMLElement) {
|
|
55
|
+
if (!defaultButton.matches(':disabled')) defaultButton.click();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (controls.filter(blocksImplicitSubmission).length > 1) return;
|
|
59
|
+
form.requestSubmit();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Gives a form-associated text field the implicit submission of a native text input: Enter in the
|
|
64
|
+
* field submits its form (see `submitImplicitly()`). Without it nothing happens, because the
|
|
65
|
+
* native input lives in the shadow root and has no form owner of its own.
|
|
66
|
+
*
|
|
67
|
+
* It acts after the keydown has reached every listener, so `preventDefault()` on the keydown
|
|
68
|
+
* cancels it. Only Enter typed in the text input counts, not Enter on a button inside the field;
|
|
69
|
+
* Enter while an input method is composing, or with Ctrl, Alt or Meta, is ignored.
|
|
70
|
+
*
|
|
71
|
+
* Call it in the constructor, before other keydown listeners are added.
|
|
72
|
+
*
|
|
73
|
+
* @param host The field. Its `form` is the form it submits.
|
|
74
|
+
* @param input The native text input inside the field.
|
|
75
|
+
*/
|
|
76
|
+
export function setupImplicitSubmission(
|
|
77
|
+
host: HTMLElement & { readonly form: HTMLFormElement | null },
|
|
78
|
+
input: () => HTMLInputElement | null | undefined,
|
|
79
|
+
) {
|
|
80
|
+
registerTextField(host);
|
|
81
|
+
setupDispatchHooks(host, 'keydown');
|
|
82
|
+
host.addEventListener('keydown', (event: KeyboardEvent) => {
|
|
83
|
+
if (
|
|
84
|
+
event.key !== 'Enter' ||
|
|
85
|
+
event.isComposing ||
|
|
86
|
+
// Safari reports the Enter that ends a composition this way.
|
|
87
|
+
event.keyCode === 229 ||
|
|
88
|
+
event.ctrlKey ||
|
|
89
|
+
event.altKey ||
|
|
90
|
+
event.metaKey
|
|
91
|
+
) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (event.composedPath()[0] !== input()) return;
|
|
95
|
+
const { form } = host;
|
|
96
|
+
if (!form) return;
|
|
97
|
+
|
|
98
|
+
afterDispatch(event, () => {
|
|
99
|
+
if (!event.defaultPrevented) submitImplicitly(form);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gate for `throttle-delay`: the returned function says whether an activation may go ahead now.
|
|
3
|
+
* The first activation opens a window of `getDelay()` milliseconds, and every activation inside
|
|
4
|
+
* that window is refused, never deferred, so nothing runs later with a stale event. With no
|
|
5
|
+
* delay (unset, zero or negative) every activation goes ahead. The delay is read on each call, so
|
|
6
|
+
* a later change to it takes effect.
|
|
7
|
+
*/
|
|
8
|
+
export function createActivationGate(
|
|
9
|
+
getDelay: () => number | undefined,
|
|
10
|
+
): () => boolean {
|
|
11
|
+
let windowStart = Number.NEGATIVE_INFINITY;
|
|
12
|
+
return () => {
|
|
13
|
+
const delay = getDelay();
|
|
14
|
+
if (typeof delay !== 'number' || !(delay > 0)) return true;
|
|
15
|
+
const now = performance.now();
|
|
16
|
+
if (now - windowStart < delay) return false;
|
|
17
|
+
windowStart = now;
|
|
18
|
+
return true;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -16,15 +16,18 @@ export async function createCacheFetch(name: string) {
|
|
|
16
16
|
|
|
17
17
|
const fetchPromise = (async () => {
|
|
18
18
|
const request = new Request(url);
|
|
19
|
+
const urlObj = new URL(request.url);
|
|
20
|
+
// The Cache API only stores http(s) requests: `put` rejects data: and blob: URLs.
|
|
21
|
+
const cacheable =
|
|
22
|
+
urlObj.protocol === 'http:' || urlObj.protocol === 'https:';
|
|
19
23
|
|
|
20
|
-
if (cache) {
|
|
24
|
+
if (cache && cacheable) {
|
|
21
25
|
const cachedResponse = await cache.match(request);
|
|
22
26
|
if (cachedResponse) {
|
|
23
27
|
return cachedResponse.text();
|
|
24
28
|
}
|
|
25
29
|
}
|
|
26
30
|
|
|
27
|
-
const urlObj = new URL(request.url);
|
|
28
31
|
const isSameOrigin = urlObj.origin === window.location.origin;
|
|
29
32
|
|
|
30
33
|
const response = await fetch(request.url, {
|
|
@@ -40,7 +43,7 @@ export async function createCacheFetch(name: string) {
|
|
|
40
43
|
|
|
41
44
|
const result = await response.text();
|
|
42
45
|
|
|
43
|
-
if (cache && response.status === 200) {
|
|
46
|
+
if (cache && cacheable && response.status === 200) {
|
|
44
47
|
await cache.put(
|
|
45
48
|
request,
|
|
46
49
|
new Response(result, {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Whether `el` is a submit button of a form: native, or a Peacock button with `type="submit"`. */
|
|
2
|
+
export function isSubmitButton(el: Element): boolean {
|
|
3
|
+
if (el instanceof HTMLButtonElement) return el.type === 'submit';
|
|
4
|
+
if (el instanceof HTMLInputElement) {
|
|
5
|
+
return el.type === 'submit' || el.type === 'image';
|
|
6
|
+
}
|
|
7
|
+
return (
|
|
8
|
+
el.localName.includes('-') &&
|
|
9
|
+
(el as Element & { type?: unknown }).type === 'submit'
|
|
10
|
+
);
|
|
11
|
+
}
|
package/src/avatar/avatar.scss
CHANGED
|
@@ -39,6 +39,11 @@
|
|
|
39
39
|
|
|
40
40
|
font-size: calc(var(--avatar-size) * 0.5) !important;
|
|
41
41
|
|
|
42
|
+
// Two initials at half of a small avatar nearly fill it; keep some room on each side.
|
|
43
|
+
:host([size='sm']) & {
|
|
44
|
+
font-size: calc(var(--avatar-size) * 0.4) !important;
|
|
45
|
+
}
|
|
46
|
+
|
|
42
47
|
.image {
|
|
43
48
|
width: 100%;
|
|
44
49
|
height: 100%;
|
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
display: none;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
// Small avatars have little room around their initials, so they overlap by a tenth of their
|
|
20
|
+
// size instead of a quarter; at a quarter the next avatar and its ring cut off the last letter.
|
|
19
21
|
:host([size='sm']) {
|
|
22
|
+
--avatar-group-overlap: calc(var(--_size) * 0.1);
|
|
20
23
|
--_size: 1.5rem;
|
|
21
24
|
}
|
|
22
25
|
|
|
@@ -32,7 +32,7 @@ interface HiddenAvatar {
|
|
|
32
32
|
*
|
|
33
33
|
* @slot - The `wc-avatar` elements.
|
|
34
34
|
*
|
|
35
|
-
* @cssprop --avatar-group-overlap - How far each avatar slides under the previous one. Defaults to a quarter of the avatar size
|
|
35
|
+
* @cssprop --avatar-group-overlap - How far each avatar slides under the previous one. Defaults to a quarter of the avatar size, a tenth at `size="sm"`.
|
|
36
36
|
* @cssprop --avatar-group-ring-width - Width of the ring between avatars. Defaults to `2px`.
|
|
37
37
|
* @cssprop --avatar-group-ring-color - Color of the ring between avatars. Defaults to `var(--color-surface)`; set it to the background the group sits on.
|
|
38
38
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { LitElement, PropertyValues } from 'lit';
|
|
2
2
|
import { property } from 'lit/decorators.js';
|
|
3
3
|
import { WithElementInternals } from '@/__internal/mixins/element-internals.js';
|
|
4
|
-
import {
|
|
4
|
+
import { createActivationGate } from '@/__internal/utils/activation-gate.js';
|
|
5
5
|
import { warnOnce } from '@/__internal/utils/warn-once.js';
|
|
6
6
|
import { MixinBase, MixinReturn } from '@/__internal/mixins/mixin.js';
|
|
7
7
|
import type {
|
|
@@ -27,9 +27,9 @@ export interface BaseButton {
|
|
|
27
27
|
|
|
28
28
|
throttleDelay?: number;
|
|
29
29
|
|
|
30
|
-
__dispatchClickWithThrottle: (event: MouseEvent | KeyboardEvent) => void;
|
|
31
|
-
|
|
32
30
|
__dispatchClick: (event: MouseEvent | KeyboardEvent) => void;
|
|
31
|
+
|
|
32
|
+
__isClickBlocked(): boolean;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export function mixinBaseButton<
|
|
@@ -67,27 +67,52 @@ export function mixinBaseButton<
|
|
|
67
67
|
disabledReason: string = '';
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
|
-
*
|
|
70
|
+
* Throttles activation by this many milliseconds: after a click, every click inside the delay
|
|
71
|
+
* is swallowed (no `click` listeners, no form submit, no toggle). When undefined (default), no
|
|
72
|
+
* throttle is applied.
|
|
71
73
|
*/
|
|
72
74
|
@property({ type: Number, attribute: 'throttle-delay' })
|
|
73
75
|
throttleDelay?: number;
|
|
74
76
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
/** Whether `throttle-delay` lets a click through now; opens a new delay when it does. */
|
|
78
|
+
private readonly __throttleAllows = createActivationGate(
|
|
79
|
+
() => this.throttleDelay,
|
|
80
|
+
);
|
|
79
81
|
|
|
80
82
|
abstract __dispatchClick: (event: MouseEvent | KeyboardEvent) => void;
|
|
81
83
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
this.throttleDelay,
|
|
88
|
-
);
|
|
89
|
-
}
|
|
84
|
+
// Mixins must have a constructor with `...args: any[]`
|
|
85
|
+
// tslint:disable-next-line:no-any
|
|
86
|
+
constructor(...args: any[]) {
|
|
87
|
+
super(...args);
|
|
88
|
+
this.addEventListener('click', this.__handleHostClick);
|
|
90
89
|
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Whether a click must be swallowed: it stops other listeners and the default action. A
|
|
93
|
+
* component returns `true` while it is soft-disabled, loading and so on.
|
|
94
|
+
*/
|
|
95
|
+
__isClickBlocked() {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Host click listener. Every activation reaches it once: a click on the inner control
|
|
101
|
+
* bubbling out, or a click fired at the host. It swallows a blocked click, and one inside the
|
|
102
|
+
* `throttle-delay` window, so later host listeners, ancestors' listeners and the form
|
|
103
|
+
* submitter (which checks `defaultPrevented`) never see it. A blocked click does not open a
|
|
104
|
+
* throttle window.
|
|
105
|
+
*/
|
|
106
|
+
private __handleHostClick = (event: MouseEvent) => {
|
|
107
|
+
if (this.__isClickBlocked() || !this.__throttleAllows()) {
|
|
108
|
+
// preventDefault() first: the dispatch hooks run the form submitter's afterDispatch()
|
|
109
|
+
// synchronously inside stopImmediatePropagation(), and it must see defaultPrevented.
|
|
110
|
+
event.preventDefault();
|
|
111
|
+
event.stopImmediatePropagation();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
this.__dispatchClick(event);
|
|
115
|
+
};
|
|
91
116
|
}
|
|
92
117
|
|
|
93
118
|
return BaseButtonElement;
|
|
@@ -152,13 +152,6 @@ export class Button extends mixinButtonLevel(
|
|
|
152
152
|
/** Cleanup returned by observerSlotChangesWithCallback for the label slot. */
|
|
153
153
|
private __labelSlotCleanup: (() => void) | null = null;
|
|
154
154
|
|
|
155
|
-
// ── Constructor ───────────────────────────────────────────────────────────
|
|
156
|
-
|
|
157
|
-
constructor() {
|
|
158
|
-
super();
|
|
159
|
-
this.addEventListener('click', this.__dispatchClickWithThrottle);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
155
|
// ── Lifecycle ─────────────────────────────────────────────────────────────
|
|
163
156
|
|
|
164
157
|
override disconnectedCallback() {
|
|
@@ -219,16 +212,12 @@ export class Button extends mixinButtonLevel(
|
|
|
219
212
|
|
|
220
213
|
// ── Private methods ───────────────────────────────────────────────────────
|
|
221
214
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (this.softDisabled || this.loading || this.skeleton) {
|
|
227
|
-
event.stopImmediatePropagation();
|
|
228
|
-
event.preventDefault();
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
215
|
+
/** A soft-disabled, loading or skeleton button swallows clicks. */
|
|
216
|
+
override __isClickBlocked() {
|
|
217
|
+
return this.softDisabled || this.loading || this.skeleton;
|
|
218
|
+
}
|
|
231
219
|
|
|
220
|
+
__dispatchClick = (event: MouseEvent | KeyboardEvent) => {
|
|
232
221
|
// Each activation reaches this listener once: either the inner button's
|
|
233
222
|
// click bubbling out, or a click fired at the host and forwarded below.
|
|
234
223
|
toggleButtonSelected(this);
|
package/src/button/fab/fab.ts
CHANGED
|
@@ -138,13 +138,6 @@ export class Fab extends mixinBaseButton(
|
|
|
138
138
|
|
|
139
139
|
@query('.button') readonly buttonElement!: HTMLElement | null;
|
|
140
140
|
|
|
141
|
-
// ── Constructor ───────────────────────────────────────────────────────────
|
|
142
|
-
|
|
143
|
-
constructor() {
|
|
144
|
-
super();
|
|
145
|
-
this.addEventListener('click', this.__dispatchClickWithThrottle);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
141
|
// ── Lifecycle ─────────────────────────────────────────────────────────────
|
|
149
142
|
|
|
150
143
|
override firstUpdated(changedProperties: PropertyValues<this>) {
|
|
@@ -169,21 +162,17 @@ export class Fab extends mixinBaseButton(
|
|
|
169
162
|
|
|
170
163
|
// ── Private methods ───────────────────────────────────────────────────────
|
|
171
164
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
// as well as prevent the default action.
|
|
176
|
-
if (
|
|
165
|
+
/** A soft-disabled, loading or skeleton button, or a disabled link, swallows clicks. */
|
|
166
|
+
override __isClickBlocked() {
|
|
167
|
+
return (
|
|
177
168
|
this.softDisabled ||
|
|
178
169
|
this.loading ||
|
|
179
|
-
(this.disabled && this.href) ||
|
|
170
|
+
(this.disabled && !!this.href) ||
|
|
180
171
|
this.skeleton
|
|
181
|
-
)
|
|
182
|
-
|
|
183
|
-
event.preventDefault();
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
172
|
+
);
|
|
173
|
+
}
|
|
186
174
|
|
|
175
|
+
__dispatchClick = (event: MouseEvent | KeyboardEvent) => {
|
|
187
176
|
// Each activation reaches this listener once: either the inner button's
|
|
188
177
|
// click bubbling out, or a click fired at the host and forwarded below.
|
|
189
178
|
if (!isLink(this)) toggleButtonSelected(this);
|