@redvars/peacock 4.8.0 → 4.8.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.
Files changed (46) hide show
  1. package/dist/checkbox.js +2 -1
  2. package/dist/checkbox.js.map +1 -1
  3. package/dist/custom-elements.json +81 -0
  4. package/dist/date-picker.js +51 -1
  5. package/dist/date-picker.js.map +1 -1
  6. package/dist/{document-editor-CSS-wACs.js → document-editor-W4pXGCEL.js} +13 -1
  7. package/dist/document-editor-W4pXGCEL.js.map +1 -0
  8. package/dist/document-editor.js +1 -1
  9. package/dist/fab-menu.js +3 -0
  10. package/dist/fab-menu.js.map +1 -1
  11. package/dist/haptics-CAaQ2E-9.js +29 -0
  12. package/dist/haptics-CAaQ2E-9.js.map +1 -0
  13. package/dist/index.js +3 -2
  14. package/dist/index.js.map +1 -1
  15. package/dist/loader-full.js +2 -1
  16. package/dist/loader-full.js.map +1 -1
  17. package/dist/loader.js +2 -1
  18. package/dist/loader.js.map +1 -1
  19. package/dist/modal.js +12 -1
  20. package/dist/modal.js.map +1 -1
  21. package/dist/radio.js +3 -1
  22. package/dist/radio.js.map +1 -1
  23. package/dist/slider.js +6 -1
  24. package/dist/slider.js.map +1 -1
  25. package/dist/src/__internal/utils/haptics.d.ts +7 -0
  26. package/dist/src/date-picker/date-picker.d.ts +6 -1
  27. package/dist/src/document-editor/document-editor.d.ts +2 -0
  28. package/dist/src/modal/modal.d.ts +4 -0
  29. package/dist/switch.js +2 -1
  30. package/dist/switch.js.map +1 -1
  31. package/dist/test/haptics.test.d.ts +1 -0
  32. package/dist/{toggle-base-DP3PSXbf.js → toggle-base-CVO9YntI.js} +3 -1
  33. package/dist/toggle-base-CVO9YntI.js.map +1 -0
  34. package/dist/tsconfig.tsbuildinfo +1 -1
  35. package/package.json +1 -1
  36. package/src/__internal/toggle-base.ts +2 -0
  37. package/src/__internal/utils/haptics.ts +32 -0
  38. package/src/date-picker/date-picker.scss +27 -0
  39. package/src/date-picker/date-picker.ts +27 -1
  40. package/src/document-editor/document-editor.ts +17 -0
  41. package/src/fab-menu/fab-menu.ts +2 -0
  42. package/src/modal/modal.ts +14 -1
  43. package/src/radio/radio.ts +2 -0
  44. package/src/slider/slider.ts +6 -1
  45. package/dist/document-editor-CSS-wACs.js.map +0 -1
  46. package/dist/toggle-base-DP3PSXbf.js.map +0 -1
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.8.0",
5
+ "version": "4.8.1",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.js",
@@ -8,6 +8,7 @@ import {
8
8
  getFormValue,
9
9
  } from '@/__internal/mixins/form-associated.js';
10
10
  import InputBase, { type ControlValidity } from '@/__internal/input-base.js';
11
+ import { haptic } from './utils/haptics.js';
11
12
 
12
13
  let toggleCount = 0;
13
14
 
@@ -191,6 +192,7 @@ export default abstract class ToggleBase extends InputBase {
191
192
  protected toggleChecked(originalEvent?: Event) {
192
193
  if (this.disabled || this.readonly) return;
193
194
  this.checked = !this.checked;
195
+ haptic('toggle');
194
196
  this.afterToggle();
195
197
  this.dispatchChange(originalEvent);
196
198
  this.containerElement?.focus();
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Short vibrations for M3 Expressive haptics. They play only where the browser supports
3
+ * `navigator.vibrate` (Chrome on Android; desktops and iOS ignore it) and only during a user
4
+ * gesture, so programmatic changes stay silent. `data-haptics="off"` on `<html>` turns them off.
5
+ */
6
+ export type HapticKind = 'tick' | 'toggle' | 'confirm';
7
+
8
+ const PATTERNS: Record<HapticKind, number | number[]> = {
9
+ /** A slider passing a step. */
10
+ tick: 4,
11
+ /** A switch, checkbox or radio changing. */
12
+ toggle: 10,
13
+ /** A menu or larger surface opening. */
14
+ confirm: [8, 40, 12],
15
+ };
16
+
17
+ export function haptic(kind: HapticKind): void {
18
+ if (typeof navigator === 'undefined' || typeof document === 'undefined') {
19
+ return;
20
+ }
21
+ if (typeof navigator.vibrate !== 'function') return;
22
+ if (document.documentElement.dataset.haptics === 'off') return;
23
+ const activation = (
24
+ navigator as Navigator & { userActivation?: { isActive: boolean } }
25
+ ).userActivation;
26
+ if (activation && !activation.isActive) return;
27
+ try {
28
+ navigator.vibrate(PATTERNS[kind]);
29
+ } catch {
30
+ // Some embedders throw instead of ignoring the call.
31
+ }
32
+ }
@@ -80,6 +80,33 @@
80
80
  gap: var(--spacing-300);
81
81
  }
82
82
 
83
+ // M3 Expressive: a new month slides in from the side it comes from (`data-enter`) on the default
84
+ // spatial spring and fades in on the effects spring. The spring tokens drop the movement for
85
+ // reduced motion.
86
+ @keyframes date-picker-month-enter {
87
+ from {
88
+ translate: var(--_month-enter-offset) 0;
89
+ opacity: 0;
90
+ }
91
+ }
92
+
93
+ .months[data-enter] {
94
+ --_month-enter-offset: 2rem;
95
+
96
+ animation: date-picker-month-enter
97
+ var(--motion-spring-default-spatial-duration)
98
+ var(--motion-spring-default-spatial-easing) backwards;
99
+ }
100
+
101
+ .months[data-enter='start'],
102
+ :host(:dir(rtl)) .months[data-enter='end'] {
103
+ --_month-enter-offset: -2rem;
104
+ }
105
+
106
+ :host(:dir(rtl)) .months[data-enter='start'] {
107
+ --_month-enter-offset: 2rem;
108
+ }
109
+
83
110
  .month {
84
111
  border-collapse: collapse;
85
112
  border-spacing: 0;
@@ -146,7 +146,8 @@ export class DatePicker extends LitElement {
146
146
  }
147
147
  }
148
148
 
149
- protected override updated() {
149
+ protected override updated(changed: Map<PropertyKey, unknown>) {
150
+ this.slideMonths(changed);
150
151
  if (this.focusAfterRender) {
151
152
  this.focusAfterRender = false;
152
153
  this.focusActive();
@@ -267,6 +268,31 @@ export class DatePicker extends LitElement {
267
268
  }
268
269
  }
269
270
 
271
+ /**
272
+ * M3 Expressive: when the shown month changes in the day view, the new month slides in from the
273
+ * side it comes from (`data-enter` on `.months`), like a tab panel.
274
+ */
275
+ private slideMonths(changed: Map<PropertyKey, unknown>) {
276
+ if (this.view !== 'day' || changed.has('view')) return;
277
+ if (!changed.has('viewYear') && !changed.has('viewMonth')) return;
278
+ const oldYear = changed.has('viewYear')
279
+ ? (changed.get('viewYear') as number | undefined)
280
+ : this.viewYear;
281
+ const oldMonth = changed.has('viewMonth')
282
+ ? (changed.get('viewMonth') as number | undefined)
283
+ : this.viewMonth;
284
+ if (!oldYear || !oldMonth) return;
285
+ const before = oldYear * 12 + oldMonth;
286
+ const now = this.viewYear * 12 + this.viewMonth;
287
+ if (before === now) return;
288
+ const months = this.renderRoot.querySelector<HTMLElement>('.months');
289
+ if (!months) return;
290
+ // Restart the animation when months change faster than it plays.
291
+ months.removeAttribute('data-enter');
292
+ months.getBoundingClientRect();
293
+ months.dataset.enter = now > before ? 'end' : 'start';
294
+ }
295
+
270
296
  private cycleView() {
271
297
  const next: Record<View, View> = {
272
298
  day: 'month',
@@ -388,6 +388,9 @@ export class DocumentEditor extends LitElement implements RichTextMentionHost {
388
388
 
389
389
  private _paginateFrame?: number;
390
390
 
391
+ /** A click below a page break is smooth-scrolling the caret into view. */
392
+ private _revealingCaret = false;
393
+
391
394
  private _changed = false;
392
395
 
393
396
  private _resizeObserver?: ResizeObserver;
@@ -981,6 +984,11 @@ export class DocumentEditor extends LitElement implements RichTextMentionHost {
981
984
  canvas.scrollTop = scroll.top * scroll.ratio;
982
985
  canvas.scrollLeft = scroll.left * scroll.ratio;
983
986
  }
987
+ // Setting scrollTop cancelled a caret reveal still scrolling: run it again at the new scale.
988
+ if (this._revealingCaret) {
989
+ this._revealingCaret = false;
990
+ requestAnimationFrame(() => this._revealCaret());
991
+ }
984
992
  this._scheduleCommentLayout();
985
993
  }
986
994
 
@@ -1087,6 +1095,15 @@ export class DocumentEditor extends LitElement implements RichTextMentionHost {
1087
1095
  const reduce = window.matchMedia?.(
1088
1096
  '(prefers-reduced-motion: reduce)',
1089
1097
  ).matches;
1098
+ // A zoom change while the smooth scroll runs resets scrollTop; it reveals the caret again.
1099
+ this._revealingCaret = true;
1100
+ canvas.addEventListener(
1101
+ 'scrollend',
1102
+ () => {
1103
+ this._revealingCaret = false;
1104
+ },
1105
+ { once: true },
1106
+ );
1090
1107
  canvas.scrollBy({
1091
1108
  top: caret.top - view.top - room,
1092
1109
  behavior: reduce ? 'auto' : 'smooth',
@@ -4,6 +4,7 @@ import { classMap } from 'lit/directives/class-map.js';
4
4
  import styles from './fab-menu.scss';
5
5
  import { I18nController } from '@/__internal/i18n/i18n-controller.js';
6
6
  import { FabMenuItem } from './fab-menu-item.js';
7
+ import { haptic } from '../__internal/utils/haptics.js';
7
8
 
8
9
  let fabMenuCount = 0;
9
10
 
@@ -91,6 +92,7 @@ export class FabMenu extends LitElement {
91
92
  private _setOpen(open: boolean) {
92
93
  if (this.open === open) return;
93
94
  this.open = open;
95
+ if (open) haptic('confirm');
94
96
  this.dispatchEvent(
95
97
  new CustomEvent('toggle', {
96
98
  detail: { open },
@@ -94,6 +94,9 @@ export class Modal extends LitElement {
94
94
  /** Peacock's strings for the close button label. */
95
95
  private readonly i18n = new I18nController(this);
96
96
 
97
+ /** Whether the current press began on the scrim, so a drag that ends there is not a scrim click. */
98
+ private _pressStartedOnScrim = false;
99
+
97
100
  private readonly overlay = new OverlayController(this, {
98
101
  surface: () => this._dialog,
99
102
  modal: true,
@@ -166,10 +169,19 @@ export class Modal extends LitElement {
166
169
  if (this.open) this._close('escape');
167
170
  }
168
171
 
172
+ /** Records where a press starts: a drag that begins in the content and ends on the scrim must not close. */
173
+ private _handleDialogPointerDown(event: PointerEvent) {
174
+ const target = event.target as Element;
175
+ this._pressStartedOnScrim = !!target.classList?.contains('dialog-wrapper');
176
+ }
177
+
169
178
  /** A click that lands outside the dialog container, not on its content, is a scrim click. */
170
179
  private _handleDialogClick(event: MouseEvent) {
171
180
  const target = event.target as Element;
172
- if (!target.classList?.contains('dialog-wrapper')) return;
181
+ const onScrim = !!target.classList?.contains('dialog-wrapper');
182
+ const pressStartedOnScrim = this._pressStartedOnScrim;
183
+ this._pressStartedOnScrim = false;
184
+ if (!onScrim || !pressStartedOnScrim) return;
173
185
  if (!this.noLightDismiss) this._close('light-dismiss');
174
186
  }
175
187
 
@@ -228,6 +240,7 @@ export class Modal extends LitElement {
228
240
  aria-busy=${this.loading ? 'true' : nothing}
229
241
  @cancel=${this._handleCancel}
230
242
  @close=${this._handleNativeClose}
243
+ @pointerdown=${this._handleDialogPointerDown}
231
244
  @click=${this._handleDialogClick}
232
245
  >
233
246
  <div
@@ -10,6 +10,7 @@ import ToggleBase from '@/__internal/toggle-base.js';
10
10
  import type { ControlValidity } from '@/__internal/input-base.js';
11
11
 
12
12
  import styles from './radio.scss';
13
+ import { haptic } from '../__internal/utils/haptics.js';
13
14
 
14
15
  type RadioDirection = 1 | -1;
15
16
 
@@ -138,6 +139,7 @@ export class Radio extends ToggleBase {
138
139
  if (!this.checked) {
139
140
  this.checked = true;
140
141
  this.uncheckSiblings();
142
+ haptic('toggle');
141
143
  this.dispatchChange(event);
142
144
  }
143
145
 
@@ -7,6 +7,7 @@ import type { ScaleSize } from '@/__internal/utils/size.js';
7
7
  import InputBase, { type ControlValidity } from '@/__internal/input-base.js';
8
8
  import sliderStyles from './slider.scss';
9
9
  import { I18nController } from '@/__internal/i18n/i18n-controller.js';
10
+ import { haptic } from '../__internal/utils/haptics.js';
10
11
 
11
12
  let sliderCount = 0;
12
13
 
@@ -255,7 +256,11 @@ export class Slider extends InputBase {
255
256
  const oldValue = this.value;
256
257
  this.value = Math.max(this.min, Math.min(this.max, steppedValue));
257
258
 
258
- if (oldValue !== this.value) this.emit('input');
259
+ if (oldValue !== this.value) {
260
+ // A tick per step on a stepped slider; a near-continuous one stays quiet.
261
+ if ((this.max - this.min) / this.step <= 20) haptic('tick');
262
+ this.emit('input');
263
+ }
259
264
  }
260
265
 
261
266
  private onMouseDown(e: MouseEvent | TouchEvent) {