actual-css 0.16.1 → 0.17.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 (119) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.md +8 -4
  3. package/components.json +19 -4
  4. package/dist/actual.full.css +464 -80
  5. package/dist/actual.full.js +2 -1
  6. package/dist/actual.full.min.css +1 -1
  7. package/docs/design-notes/app-ui.md +37 -3
  8. package/docs/design-notes/enhancement-contract.md +3 -1
  9. package/docs/navigation.json +17 -3
  10. package/docs/pages/components/accordion.md +8 -0
  11. package/docs/pages/components/alert.md +47 -17
  12. package/docs/pages/components/app-navigation.md +2 -2
  13. package/docs/pages/components/aura.md +2 -2
  14. package/docs/pages/components/badge.md +4 -3
  15. package/docs/pages/components/button.md +3 -0
  16. package/docs/pages/components/card.md +1 -1
  17. package/docs/pages/components/chat.md +2 -2
  18. package/docs/pages/components/dialog.md +36 -0
  19. package/docs/pages/components/drawer.md +52 -8
  20. package/docs/pages/components/fab.md +2 -1
  21. package/docs/pages/components/list.md +1 -1
  22. package/docs/pages/components/meter.md +1 -1
  23. package/docs/pages/components/navbar.md +27 -3
  24. package/docs/pages/components/overview.md +17 -0
  25. package/docs/pages/components/rating.md +111 -12
  26. package/docs/pages/components/segmented.md +36 -1
  27. package/docs/pages/components/shimmer.md +2 -2
  28. package/docs/pages/components/spinner.md +2 -0
  29. package/docs/pages/components/table.md +77 -0
  30. package/docs/pages/components/timeline.md +3 -1
  31. package/docs/pages/enhancements/flyout.md +83 -14
  32. package/docs/pages/enhancements/overview.md +2 -2
  33. package/docs/pages/enhancements/status-bar.md +1 -1
  34. package/docs/pages/enhancements/tabs.md +5 -0
  35. package/docs/pages/examples/overview.md +5 -1
  36. package/docs/pages/forms/ajax.md +101 -0
  37. package/docs/pages/forms/choice-cards.md +1 -1
  38. package/docs/pages/forms/color-swatches.md +31 -2
  39. package/docs/pages/forms/detached-actions.md +1 -1
  40. package/docs/pages/forms/floating-field.md +5 -3
  41. package/docs/pages/forms/form-actions.md +1 -1
  42. package/docs/pages/forms/input-policy.md +1 -1
  43. package/docs/pages/forms/joined-controls.md +9 -4
  44. package/docs/pages/forms/otp.md +2 -2
  45. package/docs/pages/forms/overview.md +1 -1
  46. package/docs/pages/forms/password.md +1 -1
  47. package/docs/pages/forms/states-and-sizes.md +1 -1
  48. package/docs/pages/forms/sticky-actions.md +1 -1
  49. package/docs/pages/forms/unsaved-changes.md +198 -0
  50. package/docs/pages/foundations/philosophy.md +2 -1
  51. package/docs/pages/foundations/theming.md +4 -3
  52. package/docs/pages/foundations/tokens.md +30 -5
  53. package/docs/pages/foundations/typography.md +1 -1
  54. package/docs/pages/guides/introduction.md +1 -1
  55. package/docs/pages/guides/progressive-enhancement.md +31 -50
  56. package/docs/pages/guides/upgrading.md +15 -2
  57. package/docs/pages/layout/app-layout.md +1 -1
  58. package/docs/pages/layout/app-shell.md +1 -1
  59. package/docs/pages/layout/choosing.md +5 -0
  60. package/docs/pages/layout/column-layout.md +23 -9
  61. package/docs/pages/layout/media.md +1 -1
  62. package/docs/pages/layout/overview.md +1 -1
  63. package/docs/pages/layout/scroll-snap.md +1 -1
  64. package/docs/pages/layout/scroll-target.md +1 -1
  65. package/docs/pages/layout/sidebar-layout.md +1 -1
  66. package/docs/pages/layout/sidebar-shell.md +68 -0
  67. package/docs/pages/patterns/code-block.md +1 -1
  68. package/docs/pages/patterns/empty-state.md +74 -0
  69. package/docs/pages/patterns/exclusive-accordion.md +1 -1
  70. package/docs/pages/patterns/hierarchical-nav.md +41 -20
  71. package/docs/pages/patterns/icon-input.md +1 -1
  72. package/docs/pages/patterns/intent-nesting.md +1 -1
  73. package/docs/pages/patterns/navigation-list.md +5 -62
  74. package/docs/pages/patterns/overview.md +1 -1
  75. package/docs/pages/patterns/stat.md +61 -0
  76. package/docs/pages/patterns/structured-lists.md +1 -1
  77. package/docs/pages/utilities/accessibility.md +32 -10
  78. package/docs/pages/utilities/flex.md +1 -1
  79. package/docs/pages/utilities/links.md +1 -1
  80. package/docs/pages/utilities/list.md +1 -1
  81. package/docs/pages/utilities/shape.md +1 -1
  82. package/docs/pages/utilities/sizing-wrapping.md +1 -1
  83. package/docs/pages/utilities/spacing.md +1 -1
  84. package/docs/pages/utilities/text.md +1 -1
  85. package/package.json +4 -1
  86. package/reserved-classes.json +2 -0
  87. package/src/cli/actual-css.js +4 -4
  88. package/src/css/components/accordion.css +10 -6
  89. package/src/css/components/alert.css +4 -0
  90. package/src/css/components/busy.css +25 -0
  91. package/src/css/components/button.css +3 -0
  92. package/src/css/components/drawer.css +37 -3
  93. package/src/css/components/flyout.css +37 -0
  94. package/src/css/components/join.css +14 -0
  95. package/src/css/components/modal.css +3 -0
  96. package/src/css/components/navbar.css +102 -38
  97. package/src/css/components/rating.css +50 -5
  98. package/src/css/components/segmented.css +25 -2
  99. package/src/css/core/theme.css +20 -1
  100. package/src/css/core/tokens.css +4 -1
  101. package/src/css/forms/color-swatch.css +38 -19
  102. package/src/css/forms/custom-select.css +10 -0
  103. package/src/css/forms/input-icon.css +5 -3
  104. package/src/css/layout/index.css +2 -0
  105. package/src/css/layout/sidebar-shell.css +67 -0
  106. package/src/css/typography/prose.css +6 -0
  107. package/src/js/ajax.js +148 -0
  108. package/src/js/dialog.js +59 -2
  109. package/src/js/dirty.js +140 -0
  110. package/src/js/dismiss.js +25 -0
  111. package/src/js/events.js +17 -0
  112. package/src/js/focus.js +12 -0
  113. package/src/js/form-submission.js +34 -0
  114. package/src/js/full.js +2 -0
  115. package/src/js/tab.js +16 -2
  116. package/src/js/validation.js +12 -4
  117. package/src/tooling/design-tokens.js +1 -0
  118. package/src/tooling/theme-contrast.js +44 -4
  119. package/tokens.json +1 -0
@@ -13,7 +13,8 @@
13
13
  * fill and focus ring, and the native radio stays visible to carry both.
14
14
  *
15
15
  * One row of equal shares (grid-auto-columns: 1fr), at least --control-size
16
- * tall so it lines up with .btn and .input. Labels wrap instead of truncating;
16
+ * tall so it lines up with .btn and .input; .vertical stacks the segments in
17
+ * one column instead. Labels wrap instead of truncating;
17
18
  * a word longer than its share breaks, so the control never overflows its
18
19
  * container. A strip too long for one row is .tabs, which scrolls.
19
20
  *
@@ -28,6 +29,9 @@
28
29
  * --segmented-fg
29
30
  * --segmented-selected-bg
30
31
  * --segmented-selected-border
32
+ *
33
+ * Internal:
34
+ * --segment-min-size
31
35
  */
32
36
  .segmented {
33
37
  --segmented-bg: var(--surface-subtle);
@@ -65,8 +69,14 @@
65
69
  own min-block-size: a <fieldset> lays its content in an anonymous box
66
70
  sized to the content, so a minimum on the track left the segments short.
67
71
  A minimum, not a height: a wrapped label grows the whole row. */
68
- min-block-size: calc(var(--control-size) - 2 * (var(--segmented-pad) + var(--border-width)));
72
+ --segment-min-size: calc(var(--control-size) - 2 * (var(--segmented-pad) + var(--border-width)));
73
+
74
+ min-block-size: var(--segment-min-size);
75
+ /* why: the block padding gives a wrapped label room, but must never push
76
+ one line past the minimum: .compact keeps the 16px font in a 32px control,
77
+ where a fixed 2px made the track 33px against .btn's 32px. */
69
78
  padding: calc(var(--space-10) / 2) var(--space-30);
79
+ padding-block: clamp(0px, (var(--segment-min-size) - 1lh) / 2, var(--space-10) / 2);
70
80
  border: 0;
71
81
  border-radius: calc(var(--segmented-radius) - var(--segmented-pad));
72
82
  background: transparent;
@@ -87,6 +97,19 @@
87
97
  box-shadow var(--duration);
88
98
  }
89
99
 
100
+ /* Vertical: one segment per row, labels at the inline start. .vertical for
101
+ radios and links, where aria-orientation is not a valid attribute; a
102
+ vertical tablist already carries aria-orientation (what the tabs runtime
103
+ reads for Up/Down), so the layout follows it and cannot disagree. */
104
+ .segmented:is(.vertical, :where([aria-orientation="vertical"])) {
105
+ grid-auto-flow: row;
106
+ }
107
+
108
+ .segmented:is(.vertical, :where([aria-orientation="vertical"])) > .segment {
109
+ justify-content: flex-start;
110
+ text-align: start;
111
+ }
112
+
90
113
  .segment > input {
91
114
  margin: 0;
92
115
  }
@@ -118,13 +118,32 @@
118
118
  --surface-opaque: var(--surface);
119
119
  --surface-solid-fg: var(--surface);
120
120
  /* The edge that tells a control apart from the surface, where --border only
121
- separates surfaces: fields, choices, switch, OTP cells, choice cards. */
121
+ separates surfaces: fields, choices, switch, OTP cells, choice cards.
122
+ Derived from the ink: the default palette clears 3:1 against the surface
123
+ in both schemes (WCAG 1.4.11: an unchecked radio is identified by its edge
124
+ alone), where --border sat at ~1.3:1. The mix is not a guarantee for every
125
+ palette: a low-contrast ink lands under 3:1, which the contrast report
126
+ (control edge) shows. Mixed into the opaque surface so a translucent theme
127
+ still draws a solid edge. The plain fallback matters: without color-mix
128
+ the var() is invalid at computed time and takes the whole border
129
+ shorthand with it, erasing every field and choice outline. */
122
130
  --border-control: var(--border);
123
131
  /* Inherited resting field fill, shared with external control bridges.
124
132
  Component state rules still own readonly, disabled and invalid fills. */
125
133
  --control-default-bg: var(--surface);
126
134
  }
127
135
 
136
+ @supports (color: color-mix(in oklab, red, white)) {
137
+ :root,
138
+ [data-theme] {
139
+ --border-control: color-mix(
140
+ in oklab,
141
+ var(--text) var(--border-control-mix),
142
+ var(--surface-opaque)
143
+ );
144
+ }
145
+ }
146
+
128
147
  /* Soft foreground hooks — the soft ink is the intent rebated toward the local
129
148
  text so it keeps 4.5:1 against the soft hover fill (the resting fill carries
130
149
  the extra margin; variants.css soft recipe). Deliberately scoped to the
@@ -28,7 +28,7 @@
28
28
  --font-width: 100%;
29
29
  --line-height: 1.5;
30
30
  --line-height-tight: 1.25;
31
- --line-height-relaxed: 1.75;
31
+ --line-height-relaxed: 1.6;
32
32
  --font-weight-normal: 400;
33
33
  --font-weight-medium: 500;
34
34
  --font-weight-semibold: 600;
@@ -206,6 +206,9 @@
206
206
  the hovered soft fill instead (theme.css); this mix is the fallback a
207
207
  theme without hooks falls back to. */
208
208
  --soft-fg-mix: 100%;
209
+ /* Control edge: the share of ink in --border-control (theme.css). Raise it
210
+ when a tinted or translucent surface pulls the edge under 3:1. */
211
+ --border-control-mix: 45%;
209
212
 
210
213
  /* Shared icon assets. Override these URLs when a strict CSP disallows data:. */
211
214
  --icon-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%238a8594' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
@@ -16,11 +16,11 @@
16
16
  * rows sit one gap apart like columns. Edge, selection ring and focus ring all
17
17
  * belong to the visible swatch.
18
18
  *
19
- * The color is arbitrary, so nothing drawn on it may assume a contrast: the
20
- * selected mark is a double ring (--text outside, --surface-opaque inside) that
21
- * reads on any fill, and a hairline edge keeps a near-white color apart from
22
- * the page. ::before opts out of forced colors to keep the color, so it takes
23
- * system colors itself there.
19
+ * The color is arbitrary, so nothing is drawn on it: the selected mark is a
20
+ * --text ring outside the swatch (::after), past a transparent gap, so it reads
21
+ * on any color and any background and leaves the color whole. A hairline edge
22
+ * keeps a near-white color apart from the page. Both pseudo-elements opt out of
23
+ * forced colors to keep the color, so they take system colors themselves there.
24
24
  *
25
25
  * Without :has() the native radio stays visible in the swatch and carries the
26
26
  * state; the ring needs :has() to mirror it.
@@ -28,16 +28,18 @@
28
28
  * Public hooks:
29
29
  * --color-swatch-value (fallback-only: the color, set per swatch)
30
30
  * --color-swatch-size
31
- * --color-swatch-radius
31
+ * --color-swatch-radius (fallback-only: set it on the palette or a swatch)
32
32
  */
33
33
  .color-swatch {
34
34
  /* The .switch track height (switch.css); a browser test holds them equal. */
35
35
  --color-swatch-size: calc(var(--choice-control-size) + 0.125em);
36
- --color-swatch-radius: var(--radius-full);
37
36
  /* Internal: what the hit area adds to the swatch, half on each side. One gap
38
37
  at most, so two neighbours never claim the same point — a wider reach let
39
38
  the next swatch take a click on this one's center. */
40
39
  --color-swatch-reach: clamp(0px, var(--control-size) - var(--color-swatch-size), var(--gap));
40
+ /* Internal: how far the selection ring (gap + stroke) reaches past the
41
+ swatch. The focus ring moves out by as much on a checked swatch. */
42
+ --color-swatch-ring: 4px;
41
43
 
42
44
  position: relative;
43
45
  display: inline-grid;
@@ -69,13 +71,29 @@
69
71
  block-size: var(--color-swatch-size);
70
72
  box-sizing: border-box;
71
73
  border: var(--border-width) solid var(--border-control);
72
- border-radius: var(--color-swatch-radius);
74
+ border-radius: var(--color-swatch-radius, var(--radius-full));
73
75
  background: var(--color-swatch-value, transparent) padding-box;
74
76
  /* The color is the content, as on .color (native.css). */
75
77
  forced-color-adjust: none;
76
- transition:
77
- border-color var(--duration),
78
- box-shadow var(--duration);
78
+ transition: border-color var(--duration);
79
+ }
80
+
81
+ /* Selection ring, drawn on every swatch and colored when checked. Its negative
82
+ margin lays it out at the swatch size: a ring wider than the grid track
83
+ would widen it and push the swatch off-center in a tight palette. */
84
+ .color-swatch::after {
85
+ content: "";
86
+ grid-area: 1 / 1;
87
+ inline-size: calc(var(--color-swatch-size) + 2 * var(--color-swatch-ring));
88
+ block-size: calc(var(--color-swatch-size) + 2 * var(--color-swatch-ring));
89
+ margin: calc(-1 * var(--color-swatch-ring));
90
+ box-sizing: border-box;
91
+ border: calc(var(--color-swatch-ring) / 2) solid transparent;
92
+ border-radius: calc(var(--color-swatch-radius, var(--radius-full)) + var(--color-swatch-ring));
93
+ pointer-events: none;
94
+ /* The gap stays transparent; forced colors would paint it. */
95
+ forced-color-adjust: none;
96
+ transition: border-color var(--duration);
79
97
  }
80
98
 
81
99
  /* Shares the swatch's cell and paints after ::before, so a native radio left
@@ -107,10 +125,8 @@
107
125
  border-color: var(--text-subtle);
108
126
  }
109
127
 
110
- .color-swatch:has(> input:checked)::before {
111
- box-shadow:
112
- inset 0 0 0 2px var(--text),
113
- inset 0 0 0 4px var(--surface-opaque);
128
+ .color-swatch:has(> input:checked)::after {
129
+ border-color: var(--text);
114
130
  }
115
131
 
116
132
  /* The ring goes around the visible swatch, not the larger hit area. */
@@ -128,6 +144,11 @@
128
144
  outline-offset: var(--focus-outline-offset);
129
145
  }
130
146
 
147
+ /* Outside the selection ring, so the two never share a stroke. */
148
+ .color-swatch:has(> input:checked:focus-visible)::before {
149
+ outline-offset: calc(var(--focus-outline-offset) + var(--color-swatch-ring));
150
+ }
151
+
131
152
  .color-swatch:disabled {
132
153
  opacity: var(--disabled-opacity);
133
154
  cursor: not-allowed;
@@ -147,10 +168,8 @@
147
168
  border-color: CanvasText;
148
169
  }
149
170
 
150
- .color-swatch:has(> input:checked)::before {
151
- box-shadow:
152
- inset 0 0 0 2px CanvasText,
153
- inset 0 0 0 4px Canvas;
171
+ .color-swatch:has(> input:checked)::after {
172
+ border-color: CanvasText;
154
173
  }
155
174
 
156
175
  .color-swatch:disabled::before {
@@ -17,6 +17,16 @@
17
17
  pointer: fine
18
18
  ) {
19
19
  @supports (appearance: base-select) {
20
+ /* why: base-select brings a UA padding-block (0.25em) that the native
21
+ select lacks; with a 24px line it pushed a .compact select to 34px
22
+ against --control-size 32px. min-block-size and align-items already
23
+ center the value. Zero specificity: the reset only has to beat the UA
24
+ sheet, and at (0,3,0) it erased the headroom .floating-field reserves,
25
+ so the floated label sat on the value. */
26
+ :where(.select:not([multiple], .native, [size]:not([size="1"]))) {
27
+ padding-block: 0;
28
+ }
29
+
20
30
  .select:not([multiple], .native, [size]:not([size="1"])) {
21
31
  appearance: base-select;
22
32
  align-items: center;
@@ -49,14 +49,16 @@
49
49
  block-size: 1em;
50
50
  }
51
51
 
52
- /* Leading: input gets start padding to clear the icon */
52
+ /* Leading: input gets start padding to clear the icon. The text sits half a
53
+ pad from the icon: a full pad, the same as the icon's inset from the edge,
54
+ read as two separate items rather than an icon labelling its field. */
53
55
  .input-icon > :first-child:where(:not(.input)) ~ .input {
54
- padding-inline-start: calc(var(--control-pad-x) * 2 + var(--input-icon-size));
56
+ padding-inline-start: calc(var(--control-pad-x) * 1.5 + var(--input-icon-size));
55
57
  }
56
58
 
57
59
  /* Trailing: the input has a following sibling → end padding. Not tied to the
58
60
  input being first child, so a leading icon does not disable the trailing
59
61
  icon's reserved space. */
60
62
  .input-icon > .input:where(:not(:last-child)) {
61
- padding-inline-end: calc(var(--control-pad-x) * 2 + var(--input-icon-size));
63
+ padding-inline-end: calc(var(--control-pad-x) * 1.5 + var(--input-icon-size));
62
64
  }
@@ -8,6 +8,7 @@
8
8
  * .center centered content column
9
9
  * .frame fixed-ratio media frame
10
10
  * .app-shell full-height application shell
11
+ * .sidebar-shell persistent side region + main, each scrolling; drawer below 64rem
11
12
  * .app-layout topbar + scrolling main + bottom/side app navigation
12
13
  * .media fixed media + flexible content
13
14
  * .switcher peer regions: all horizontal or all stacked
@@ -31,6 +32,7 @@
31
32
  @import "./center.css";
32
33
  @import "./frame.css";
33
34
  @import "./app-shell.css";
35
+ @import "./sidebar-shell.css";
34
36
  @import "./app-layout.css";
35
37
  @import "./media.css";
36
38
  @import "./switcher.css";
@@ -0,0 +1,67 @@
1
+ /*
2
+ * Sidebar shell — a persistent side region beside the main one on a wide
3
+ * viewport, each scrolling on its own; below 64rem the side region hides and
4
+ * the document scrolls. The page mirrors the navigation into a dialog.drawer.
5
+ *
6
+ * Docs: docs/pages/layout/sidebar-shell.md
7
+ * Design note: docs/design-notes/app-ui.md#sidebar-shell
8
+ *
9
+ * Structure: two direct children, an `aside` (side) and one other element
10
+ * (main). The breakpoint is a literal; a custom property cannot sit in a
11
+ * media condition.
12
+ *
13
+ * Public hooks:
14
+ * --sidebar-shell-size
15
+ */
16
+ .sidebar-shell {
17
+ --sidebar-shell-size: 16rem;
18
+ }
19
+
20
+ /* Hidden below, never shown above: the aside keeps the author's display (an
21
+ aside.stack). Where range syntax is unsupported the rule drops and the
22
+ sidebar stacks above the content, still reachable. Screen only: a printed
23
+ page is narrower than 64rem, and print never hides framework content. */
24
+ @media screen and (width < 64rem) {
25
+ .sidebar-shell > aside {
26
+ display: none;
27
+ }
28
+ }
29
+
30
+ @media (width >= 64rem) {
31
+ .sidebar-shell {
32
+ display: grid;
33
+ grid-template-columns: var(--sidebar-shell-size) minmax(0, 1fr);
34
+ block-size: var(--viewport-block);
35
+ }
36
+
37
+ /* Each region is its own scroller, so the sidebar stays put. min-block-size
38
+ lets the grid row hold them to the viewport instead of their content. */
39
+ .sidebar-shell > * {
40
+ min-block-size: 0;
41
+ overflow: auto;
42
+ overscroll-behavior: contain;
43
+ }
44
+
45
+ .sidebar-shell > aside {
46
+ grid-column: 1;
47
+ grid-row: 1;
48
+ }
49
+
50
+ .sidebar-shell > :not(aside) {
51
+ grid-column: 2;
52
+ grid-row: 1;
53
+ }
54
+ }
55
+
56
+ /* Paper is one flow, side then main, nothing clipped by a scroller. Leaving
57
+ the sidebar off the page is the application's print stylesheet. */
58
+ @media print {
59
+ .sidebar-shell {
60
+ display: block;
61
+ block-size: auto;
62
+ }
63
+
64
+ .sidebar-shell > * {
65
+ overflow: visible;
66
+ }
67
+ }
@@ -163,6 +163,9 @@
163
163
 
164
164
  /* Overflow containment is global on `pre` (base.css); only the box is prose's. */
165
165
  .prose :where(pre) {
166
+ /* why: code is scanned line by line, not read as running text; the prose
167
+ reading leading spread a block to ~1.7 lines per line of code. */
168
+ line-height: var(--line-height);
166
169
  padding: 1em;
167
170
  border: var(--border-width) solid var(--border);
168
171
  border-radius: var(--radius);
@@ -192,6 +195,9 @@
192
195
  }
193
196
 
194
197
  .prose :where(table) {
198
+ /* why: tables are scanned cell by cell, like code; the reading leading made
199
+ every wrapped cell tall and loose. */
200
+ line-height: var(--line-height);
195
201
  inline-size: 100%;
196
202
  border-collapse: collapse;
197
203
  }
package/src/js/ajax.js ADDED
@@ -0,0 +1,148 @@
1
+ /*
2
+ * Ajax — optional fetch transport for HTML forms.
3
+ *
4
+ * Opt in with data-enhance="ajax". A submission the module can reproduce
5
+ * faithfully is sent with fetch; anything else is left to the browser:
6
+ * method="dialog", enctype="text/plain", a target other than the page itself,
7
+ * or a cross-origin action. Refusing beats approximating, and the native path
8
+ * always works.
9
+ *
10
+ * Transport: one request at a time per form, aria-busy on the form and the
11
+ * submitter, the request aborted when the form leaves the page, redirects
12
+ * followed by navigation like a native submission.
13
+ *
14
+ * Display, kept apart: with data-ajax-target, an HTML response (2xx or 422)
15
+ * replaces the target's content. Everything else only reaches the events.
16
+ * The module never decides that a response means "saved".
17
+ */
18
+
19
+ import { registerEnhancement } from "./enhance.js";
20
+ import { EVENTS } from "./events.js";
21
+ import { submissionSetting, submissionTarget } from "./form-submission.js";
22
+
23
+ const BUSY = "aria-busy";
24
+ const ENCODINGS = new Set(["application/x-www-form-urlencoded", "multipart/form-data"]);
25
+
26
+ function isFormElement(el) {
27
+ return typeof HTMLFormElement !== "undefined" && el instanceof HTMLFormElement;
28
+ }
29
+
30
+ // URL encoding as a native submission does it: CRLF newlines, and a file
31
+ // sent as its name. URLSearchParams alone would send "[object File]".
32
+ function urlEncoded(data) {
33
+ const params = new URLSearchParams();
34
+ const crlf = (value) => value.replace(/\r\n?|\n/g, "\r\n");
35
+ for (const [name, value] of data) {
36
+ params.append(crlf(name), crlf(typeof value === "string" ? value : value.name));
37
+ }
38
+ return params;
39
+ }
40
+
41
+ /** Builds the request, or returns null for a submission left to the browser. */
42
+ function requestFor(form, submitter) {
43
+ const method = submissionSetting(form, submitter, "method");
44
+ const enctype = submissionSetting(form, submitter, "enctype");
45
+ const target = submissionTarget(form, submitter).toLowerCase();
46
+ if (method !== "get" && method !== "post") return null;
47
+ if (target !== "" && target !== "_self") return null;
48
+ if (method === "post" && !ENCODINGS.has(enctype)) return null;
49
+ const url = new URL(submissionSetting(form, submitter, "action"));
50
+ // Cross-origin would need CORS and send no cookies: the native submission
51
+ // works where fetch would fail. (file: pages compare "null" to "null".)
52
+ if (url.origin !== form.ownerDocument.location.origin) return null;
53
+
54
+ const data = new FormData(form, submitter);
55
+ // De facto marker servers use to answer with a fragment instead of a page.
56
+ const init = { method, headers: { Accept: "text/html", "X-Requested-With": "XMLHttpRequest" } };
57
+ if (method === "get") url.search = urlEncoded(data).toString();
58
+ else init.body = enctype === "multipart/form-data" ? data : urlEncoded(data);
59
+ return { url, init };
60
+ }
61
+
62
+ function targetElement(root, selector) {
63
+ try {
64
+ return root.querySelector(selector);
65
+ } catch {
66
+ // A configuration error, not a failed request: keep it out of ajax-error.
67
+ console.warn(`data-ajax-target is not a valid selector: "${selector}"`);
68
+ return null;
69
+ }
70
+ }
71
+
72
+ function display(form, root, response) {
73
+ const selector = form.dataset.ajaxTarget;
74
+ if (!selector || !(response.ok || response.status === 422)) return null;
75
+ const type = response.headers.get("Content-Type")?.toLowerCase() ?? "";
76
+ if (!type.includes("text/html")) return null;
77
+ const target = targetElement(root, selector);
78
+ if (!target) return null;
79
+ // The clone leaves the original body readable by event listeners.
80
+ return response
81
+ .clone()
82
+ .text()
83
+ .then((html) => {
84
+ target.innerHTML = html;
85
+ });
86
+ }
87
+
88
+ function connectForm(form) {
89
+ const root = form.getRootNode();
90
+ const lifetime = new AbortController();
91
+ let pending = false;
92
+
93
+ // A target that contains the form (the server re-renders the whole form)
94
+ // detaches it; the root still reaches document listeners.
95
+ const emit = (name, detail) => {
96
+ const origin = form.isConnected ? form : root;
97
+ origin.dispatchEvent(new CustomEvent(name, { bubbles: true, detail: { form, ...detail } }));
98
+ };
99
+
100
+ const send = async (submitter, { url, init }) => {
101
+ pending = true;
102
+ // Restored afterwards, not removed: an authored aria-busy="false" is what
103
+ // keeps a last-child .spinner hidden at rest (busy.css).
104
+ const busy = [form, submitter].filter(Boolean).map((el) => [el, el.getAttribute(BUSY)]);
105
+ for (const [el] of busy) el.setAttribute(BUSY, "true");
106
+ emit(EVENTS.ajaxStart, { submitter });
107
+ try {
108
+ const response = await fetch(url, { ...init, signal: lifetime.signal });
109
+ if (response.redirected) {
110
+ emit(EVENTS.ajaxResponse, { submitter, response });
111
+ location.assign(response.url);
112
+ return;
113
+ }
114
+ await display(form, root, response);
115
+ emit(EVENTS.ajaxResponse, { submitter, response });
116
+ } catch (error) {
117
+ // An abort means the form left the page: nobody is listening.
118
+ if (!lifetime.signal.aborted) emit(EVENTS.ajaxError, { submitter, error });
119
+ } finally {
120
+ pending = false;
121
+ for (const [el, rest] of busy) {
122
+ if (rest === null) el.removeAttribute(BUSY);
123
+ else el.setAttribute(BUSY, rest);
124
+ }
125
+ }
126
+ };
127
+
128
+ // Listen on the root, after the form's own listeners: validation (or the
129
+ // application) cancels or stops an invalid submission before it gets here,
130
+ // whatever order the enhancers started in.
131
+ root.addEventListener(
132
+ "submit",
133
+ (event) => {
134
+ if (event.target !== form || event.defaultPrevented) return;
135
+ // Decide first: a submission left to the browser (a dialog's Cancel)
136
+ // goes through even while a request runs.
137
+ const request = requestFor(form, event.submitter);
138
+ if (!request) return;
139
+ event.preventDefault();
140
+ if (!pending) send(event.submitter, request); // one request at a time
141
+ },
142
+ { signal: lifetime.signal },
143
+ );
144
+
145
+ return () => lifetime.abort();
146
+ }
147
+
148
+ registerEnhancement("ajax", (el) => (isFormElement(el) ? connectForm(el) : undefined));
package/src/js/dialog.js CHANGED
@@ -15,6 +15,8 @@
15
15
  * indicator on backdrop click. Escape and explicit close requests always
16
16
  * close, unless an application cancels the actual:dialog-cancel event, and
17
17
  * closedby="none" keeps its native meaning (no Escape, no backdrop close).
18
+ * data-dialog-open opens the dialog once the document is parsed, in its
19
+ * data-dialog-modal mode; with `open` it upgrades the no-JS rendering.
18
20
  *
19
21
  * View transitions (opt-in via data-dialog-view-transition):
20
22
  * When the browser supports document.startViewTransition and the user allows
@@ -30,9 +32,12 @@
30
32
  import { registerCommands, targetFor } from "./command.js";
31
33
  import enhance, { registerEnhancement } from "./enhance.js";
32
34
  import { EVENTS } from "./events.js";
35
+ import { submissionSetting } from "./form-submission.js";
36
+ import { afterParse } from "./parsed.js";
33
37
  import { CLASSES } from "./selectors.js";
34
38
 
35
39
  const dialogMap = new WeakMap();
40
+ const autoOpened = new WeakSet();
36
41
  const wiredDialogs = new Set();
37
42
  const delegatedDialogs = new WeakSet();
38
43
  // HTML has no non-modal dialog keyword, so non-modal opening is the custom
@@ -400,7 +405,9 @@ function handleDialogSubmit(event) {
400
405
  const form = event.target;
401
406
 
402
407
  if (!(form instanceof HTMLFormElement)) return;
403
- if (form.method.toLowerCase() !== "dialog") return;
408
+ // formmethod on the submitter wins, as natively: a Save posting from a
409
+ // method="dialog" form must not close the dialog.
410
+ if (submissionSetting(form, event.submitter, "method") !== "dialog") return;
404
411
  if (!canViewTransition(dialog, dialogMap.get(dialog)?.restoreFocusTo)) return;
405
412
 
406
413
  event.preventDefault();
@@ -440,6 +447,9 @@ function handleDialogCancel(event) {
440
447
 
441
448
  function handleDialogClose(event) {
442
449
  const dialog = event.currentTarget;
450
+ // close is queued as a task: a dialog closed then reopened in the same task
451
+ // receives it while open again, and the cleanup would undo the new opening.
452
+ if (dialog.open) return;
443
453
  const state = dialogMap.get(dialog);
444
454
 
445
455
  clearVtName(dialog);
@@ -508,9 +518,56 @@ function ensureDialogWired(dialog) {
508
518
  return state;
509
519
  }
510
520
 
521
+ /* The close() that precedes an upgrade queues a close event for a dialog
522
+ nobody closed. Window capture stops it before downstream listeners, even
523
+ though close does not bubble; earlier window capture listeners still see it. */
524
+ function swallowNextClose(dialog) {
525
+ const view = dialog.ownerDocument.defaultView;
526
+ if (!view) return;
527
+ const { controller } = ensureDialogWired(dialog);
528
+ const swallow = (event) => {
529
+ if (event.target !== dialog) return;
530
+ view.removeEventListener("close", swallow, true);
531
+ if (dialog.open) event.stopImmediatePropagation();
532
+ };
533
+ // A detached dialog's queued event no longer passes through the window.
534
+ view.addEventListener("close", swallow, { capture: true, signal: controller.signal });
535
+ }
536
+
537
+ /* data-dialog-open: open once per element, for a page the server rendered
538
+ with the dialog showing. A dialog that also carries `open` is already shown
539
+ without JavaScript, non-modally; the runtime upgrades it to its modal mode.
540
+ A reinserted node does not reopen; a new node does. */
541
+ function autoOpen(dialog) {
542
+ if (!boolData(dialog, "data-dialog-open") || autoOpened.has(dialog)) return;
543
+ if (dialog.open) {
544
+ // Already modal (a command came first), or meant to stay non-modal.
545
+ if (isModalOpen(dialog) || !isModal(dialog)) {
546
+ autoOpened.add(dialog);
547
+ return;
548
+ }
549
+ // showModal() throws on a dialog that is open non-modally.
550
+ swallowNextClose(dialog);
551
+ dialog.close();
552
+ }
553
+ openDialog(dialog);
554
+ if (dialog.open) {
555
+ // No trigger owns this opening: leave focus restoration to the browser,
556
+ // and do not morph the closing dialog into the previously active element.
557
+ dialogMap.get(dialog).restoreFocusTo = null;
558
+ autoOpened.add(dialog);
559
+ }
560
+ }
561
+
511
562
  function connectDialog(dialog) {
512
563
  ensureDialogWired(dialog);
513
- return () => disconnectDialog(dialog);
564
+ // Once parsed: the title that labels the dialog and the control that takes
565
+ // initial focus may not exist yet when its start tag connects.
566
+ const cancelOpen = afterParse(dialog, () => autoOpen(dialog));
567
+ return () => {
568
+ cancelOpen();
569
+ disconnectDialog(dialog);
570
+ };
514
571
  }
515
572
 
516
573
  function ensureDelegatedDialog(dialog) {