studio-engine 0.143.2 → 0.144.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d143f0f1859ecc3107721013965ab60cfdc991f7bee318fee274361278e98cc1
4
- data.tar.gz: 32c057de7990555e9a8ff3a54963ba6916e0bba30fc26489a46c783def9fb5f8
3
+ metadata.gz: c43f1f30b1b77ed84d5f243c25d5210b01008ecbb43a96dc5c55a8ccf08b53d7
4
+ data.tar.gz: 71623f99204ce7afb7c6381186d632045764fd385fb08fe1af08c1eaea1a8fbc
5
5
  SHA512:
6
- metadata.gz: d0857709512f2a2180b6d1c25aeb9c886ba6708550cc00560243d5881d0422e900c77145823e31018aab7d334133deb8087fc3cbcaebfa79a5ef332ce15ecef1
7
- data.tar.gz: d500cb012588520e9c8d76974f9a5f8189560288216b584a6712a609393b0dbed749e10bf1b33bb590932dbfc4cadf632a86ad448053dafa476fbb956a78128b
6
+ metadata.gz: 671142acad89b5deee37b401108764b0d516cdfffb1a30ff0763ca7b0220780b8008f61d2846397daf63a728e25f19fff0e3af23fba725032b357ce837a2b330
7
+ data.tar.gz: 43e6e425adc3e096d4215f2d8099148ba6ebd63f2fbba9f0f5f17b3784ba4d2b38bd177dc3a8ed6ad2b6daaeec0e226e43ad424f590d90f826a301c49a56e664
data/CHANGELOG.md CHANGED
@@ -4,6 +4,28 @@ The format is [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). This pro
4
4
 
5
5
  ## Unreleased
6
6
 
7
+ ## 0.144.1 — 2026-11-27
8
+
9
+ ### Fixed
10
+
11
+ - **`Studio::SystemTestCase` system tests pass on Chrome 158.** Capybara 3.40.0's Chrome and Edge `reset!` ends with `Storage.clearDataForOrigin` for origin `"*"`, which Chrome 158 refuses ("invalid argument: * is not a valid URL"), so every system test errored in teardown. A prepend on Capybara's Chrome and Edge specializations now names the real origins: each http(s) origin the driver visited since the last reset, the page it is on at reset, and `Capybara.app_host`. Cookies still clear for every origin, through `Network.clearBrowserCookies`. No change for a consumer that adopts the base: it loads with `require "studio/system_test_case"`. An app on Chrome 158 needs the base (or its own patch) before shared CI drops its `chrome-version: "157"` pin.
12
+
13
+ ## 0.144.0 — 2026-11-11
14
+
15
+ ### Added
16
+
17
+ - **`Studio::SystemTestCase`, one shared system-test base.** The browser harness fixes that each app carried on its own now ship in the gem: the window resets to `desktop_size` before every test, `evaluate_async_script` gives back the script timeout Capybara 3.40.0 leaves at 2s, a phone viewport's DevTools emulation is cleared after every test, and Selenium gets a 180s read timeout. Helpers `phone_viewport!`, `assert_viewport_width` and `clear_emulation` come with it. An app adopts with `require "studio/system_test_case"` and `class ApplicationSystemTestCase < Studio::SystemTestCase`, keeps its own settings, setup, helpers and tests, and opts out of one hook by name with `skip_harness_hook :name, reason:`, which is logged. `lib/studio.rb` does not load it. See `docs/SYSTEM_TESTS.md`.
18
+ - **`Studio::TableComponent` takes `caption:` and `aria_label:`, and its scroll wrapper is keyboard-scrollable.** The wrapper is always `tabindex="0"` with an inset focus ring, so a keyboard user can focus it and scroll a wide table with the arrow keys. `caption:` renders a visually hidden `<caption>`; `aria_label:` labels the table without one. Either one also makes the wrapper a labelled `role="region"`. Pass one on every table.
19
+ - **`Studio::ButtonComponent` gains `tone: :outline`** (`btn-outline`: a primary border, unfilled until hover), drawn in light and dark on `/admin/style` and pinned by the screenshot baselines.
20
+
21
+ ### Changed
22
+
23
+ - **`Studio::ButtonComponent`'s default `md` size puts its label on the type scale.** It now adds the new `btn-md` utility: body size (1x `--type-base`, 16px; 14px under `type-dense`) at a 1.25 line height, so the button grows no taller. A bare `.btn` keeps `text-sm`, so hand-built buttons do not move. A host that relied on the 14px component label sees it at 16px.
24
+
25
+ ### Fixed
26
+
27
+ - **On-demand modals (`Studio.modals`) are hardened before Turf adopts them.** A fetch that does not answer within `Studio.modals.fetch_timeout_ms` (default 8000) is aborted and shows the error card, which now has Try again and a Close that works on a card opened `dismissible: false`. The fragment cache is dropped when the session changes by Turbo visit (sign-in, sign-out, a user switch), keyed on `Studio::Modals.session_marker`, so no cached fragment carries a dead CSRF token or the prior user's data. A fragment containing a `<script>` is refused, not inserted (the reason shows on the card in development and test). The loading card is `role="status"`. Prefetch also runs after each Turbo visit. No change for a consumer: `$store.modals.open(id, props, opts)` is unchanged; set `fetch_timeout_ms` in the initializer to change the wait.
28
+
7
29
  ## 0.143.2 — 2026-11-10
8
30
 
9
31
  ## 0.143.1 — 2026-11-10
data/Gemfile CHANGED
@@ -29,6 +29,13 @@ group :development, :test do
29
29
  # Rails.application directly rather than growing Rails-app scaffolding inside a
30
30
  # test fixture. Not a runtime dependency — consuming apps bring their own server.
31
31
  gem "puma", ">= 6.0"
32
+
33
+ # Capybara and selenium-webdriver drive Studio::SystemTestCase's own tests
34
+ # (test/lib/system_test_case_test.rb, test/system/). The gem does not declare
35
+ # them: lib/studio.rb never loads lib/studio/system_test_case.rb, and every
36
+ # host that inherits the base bundles both in its own test group.
37
+ gem "capybara", ">= 3.39"
38
+ gem "selenium-webdriver", ">= 4.20"
32
39
  end
33
40
 
34
41
  # solana-studio backs the style guide's THREE web3 specimens (Connect wallet and
data/README.md CHANGED
@@ -55,6 +55,7 @@ resolved.
55
55
  - **Connections**: generic OAuth 2.0 tooling to connect a third-party account. A host declares each provider with `Studio.define_connection` (endpoints, static or per-account; scopes; form or HTTP Basic client auth; a rotating, cached or client-credentials refresh policy). The engine gives it `Studio::Connection` (one table, tokens encrypted, `bin/rails g studio:connections`), the connect, callback and disconnect actions (`Studio::ConnectionFlow`) behind the host's admin guard, `Studio.draw_connection_routes`, an exact-key log filter, and a stand-in for the e2e lane. The tooling names no provider; the engine's own definitions are the Integrations below. See [`docs/CONNECTIONS.md`](docs/CONNECTIONS.md).
56
56
  - **Integrations**: ready connection definitions on that tooling for QuickBooks, Egnyte and Microsoft 365: the URLs, scopes, token behaviour and a minimal describe, never a reader. A host turns one on with `Studio.enable_integration :quickbooks, env: { client_id: "...", client_secret: "..." }` (env var names) and mounts the admin page with `Studio.draw_integrations_routes(self)`: a list with each status and Connect, and one page per integration with Disconnect and a host panel for the host's own live read. See [`docs/INTEGRATIONS.md`](docs/INTEGRATIONS.md).
57
57
  - **Surveys**: define a survey in app code (`config/surveys/*.rb`, `Studio.define_survey`) with six question types — emoji scale, 1–5 rating, single and multiple choice, short and long text — and the engine serves it at `/surveys/:slug`: one question per screen, autosave, resume, keyboard shortcuts, and a no-JavaScript fallback. `config.on_survey_completed` and `config.survey_ref_resolver` let the app fire its own goals and attribute email arrivals. An admin panel at `/admin/surveys` breaks results down per question and exports CSV. Opt in with `config.draw_survey_routes = true` and `config.draw_admin_survey_routes = true`. See [`docs/SURVEYS.md`](docs/SURVEYS.md).
58
+ - **System tests**: `Studio::SystemTestCase`, the one Capybara base every host's `ApplicationSystemTestCase` inherits (`require "studio/system_test_case"`). It resets the window before each test, restores the script timeout Capybara 3.40.0's `evaluate_async_script` leaves at 2s, clears phone emulation after each test, and gives Selenium a 180s read timeout. An app keeps its own settings (`phone_width`), setup, helpers and tests, and opts out of one hook by name with `skip_harness_hook`, which is logged. See [`docs/SYSTEM_TESTS.md`](docs/SYSTEM_TESTS.md).
58
59
 
59
60
  ## Tailwind
60
61
 
@@ -309,6 +310,7 @@ size and state live (Components section), and the gallery has a preview per stat
309
310
  <%= render Studio::ButtonComponent.new(label: "Save", type: "submit") %>
310
311
  <%= render Studio::ButtonComponent.new(label: "Delete", tone: :danger, size: :sm) %>
311
312
  <%= render Studio::ButtonComponent.new(label: "Docs", tone: :quiet, href: docs_path) %>
313
+ <%= render Studio::ButtonComponent.new(label: "Export", tone: :outline) %>
312
314
  <%= render Studio::ButtonComponent.new(label: "Save", type: "submit", loading: true, busy_label: "Saving…") %>
313
315
 
314
316
  <%= render(Studio::EmptyStateComponent.new(detail: "Invite someone to start.")) do |empty| %>
@@ -318,6 +320,7 @@ size and state live (Components section), and the gallery has a preview per stat
318
320
 
319
321
  <%= render Studio::TableComponent.new(
320
322
  rows: @users, sort: params[:sort], direction: params[:direction], empty: "No users yet.",
323
+ caption: "Team members",
321
324
  columns: [
322
325
  { key: :name, label: "User", sortable: true },
323
326
  { key: :created_at, label: "Joined", align: :right, cell: ->(u) { l(u.created_at.to_date) } }
@@ -326,11 +329,19 @@ size and state live (Components section), and the gallery has a preview per stat
326
329
 
327
330
  - **Button.** Tones map onto the shipped utilities: `primary` is `btn-primary`,
328
331
  `secondary` is `btn-secondary` (the success role, or the app's
329
- `--btn-secondary-*` retune), `quiet` is `btn-neutral`, `danger` is `btn-danger`.
330
- Sizes are `btn-sm`, bare `btn` and `btn-lg`. `loading:` uses the aria-busy
332
+ `--btn-secondary-*` retune), `quiet` is `btn-neutral`, `danger` is `btn-danger`,
333
+ `outline` is `btn-outline` (a primary border, unfilled until hover).
334
+ Sizes are `btn-sm`, `btn-md` and `btn-lg`. `btn-md` puts the medium label on
335
+ the type scale at body size (1x `--type-base`); a bare `btn` keeps `text-sm`,
336
+ so hand-built buttons do not move. `loading:` uses the aria-busy
331
337
  contract (docs/LOADER.md): the button is `aria-busy` and disabled and shows the
332
338
  pending spinner. A submit button always carries the hidden spinner, so a Turbo
333
339
  form that marks itself busy shows it with no script. A link cannot be loading.
340
+ **A busy form hides the icon on every button in it**, not only on the submit
341
+ button: the aria-busy contract hides every `[data-loader-idle]` under the busy
342
+ element, and the icon slot is idle content. A Cancel or Back button with an
343
+ icon inside the form loses its icon while the form submits. Put such a button
344
+ outside the form, or give it no icon.
334
345
  - **Empty state.** The `.empty-state` card, the message at `type-body`, the
335
346
  detail at `type-fine`, an optional icon, and at most one action, which is a
336
347
  `Studio::ButtonComponent`. `framed: false` drops the card. The
@@ -341,7 +352,10 @@ size and state live (Components section), and the gallery has a preview per stat
341
352
  `sort_url:` takes a callable for another URL shape. No rows renders one row with
342
353
  a bare empty state. On a phone the table scrolls sideways, as every engine table
343
354
  does; `min_width:` sets where. `sticky_header: true` adds
344
- `data-sticky-table-header`.
355
+ `data-sticky-table-header`. Give every table a name: `caption:` renders a
356
+ visually hidden `<caption>`, `aria_label:` labels the table without one. The
357
+ scroll wrapper is always `tabindex="0"`, so a keyboard user can scroll it, and a
358
+ name also makes it a labelled `role="region"`.
345
359
 
346
360
  ### The page header — `studio/_page_header`
347
361
 
@@ -737,11 +751,22 @@ change: `$store.modals.open("contest_rules", props)` works as before.
737
751
  |--------|---------|
738
752
  | `partial:` | The content partial, as the layout rendered it. The two rules below still apply (one root, no double quote in `x-data`). |
739
753
  | `auth:` | `:any` (default) or `:user`. A `:user` modal answers `401` to a signed-out request and is never prefetched for one. |
740
- | `prefetch:` | `true` fetches the modal when the browser idles after load (`requestIdleCallback`, else a timeout), so it opens with no wait. Use it for a modal opened on most visits. |
754
+ | `prefetch:` | `true` fetches the modal when the browser idles after load and after every Turbo visit (`requestIdleCallback`, else a timeout), so it opens with no wait. Use it for a modal opened on most visits. |
741
755
  | `locals:` | A Hash, a Symbol naming a helper the view answers (an existing `*_modal_locals` helper), or a callable taking the view. Resolved per request, in the view. |
742
756
 
743
757
  Ids are lower-case letters, digits, `-` and `_`.
744
758
 
759
+ **Locals run in the modal endpoint, not on the page.** The view that resolves
760
+ `locals:` belongs to `Studio::ModalsController`, so it has the helpers and the
761
+ session (`current_user`, `logged_in?`) but none of the page's instance
762
+ variables. A `*_modal_locals` helper that reads `@contest` or any other
763
+ page-scoped ivar gets `nil` there. Such a helper cannot move as it is: derive
764
+ what it needs from the session or the user, or keep that modal in the layout.
765
+
766
+ **The fetch timeout.** `Studio.modals.fetch_timeout_ms` (default `8000`) is how
767
+ long the host waits for a fragment. Set it in the initializer:
768
+ `Studio.modals.fetch_timeout_ms = 12_000`.
769
+
745
770
  **What happens.** The host advertises the registered ids on its template
746
771
  (`data-modal-host-remote-value`). `open(id)` for one that is not in the page
747
772
  yet fetches `GET /_studio/modals/<id>` (`Studio::ModalsController`), wraps the
@@ -749,12 +774,41 @@ fragment in the same `<template x-if>` a layout would render, appends it to the
749
774
  card, and opens. A fetch that answers within 150 ms opens with no loading state.
750
775
  A slower one opens the card with the engine's processing card, held at least
751
776
  `StudioModals.MIN_LOAD_MS`, then the content replaces it. A failed fetch (a
752
- network error, a `500`, a `401`, or a host filter's redirect) shows the
753
- engine's error card, whose button closes; the next open tries again.
754
-
755
- The fragment is held for the page's lifetime, across Turbo visits: a later open
756
- inserts it from memory into the new page's host, once, with no second request.
757
- A fetch that lands after a Turbo visit opens nothing on the new page.
777
+ network error, a `500`, a `401`, a host filter's redirect, or no answer within
778
+ `fetch_timeout_ms`) shows the engine's error card. **Try again** re-runs the
779
+ fetch with the card in place; **Close** closes it, even on a card opened
780
+ `dismissible: false` (Escape and the backdrop still respect that flag). The next
781
+ open tries again too. The loading card is `role="status"` (`aria-live`
782
+ polite); the error card is `role="alert"`.
783
+
784
+ **A fragment with a `<script>` is refused**, never inserted: it shows the error
785
+ card. In development and test the card names the reason; in production it shows
786
+ the generic line. Nothing is logged to the console either way; the endpoint
787
+ records an `ErrorLog` row instead, once per modal id per process. Put behaviour in a module or a Stimulus
788
+ controller.
789
+
790
+ The fragment is held across Turbo visits: a later open inserts it from memory
791
+ into the new page's host, once, with no second request. A fetch that lands
792
+ after a Turbo visit opens nothing on the new page.
793
+
794
+ **The cache follows the session.** A fragment can carry the viewer's data and a
795
+ form's CSRF token. The host advertises a session marker
796
+ (`Studio::Modals.session_marker`: a digest of the session id and the user id
797
+ under `Studio.session_key`). The browser compares it on every open and every
798
+ `turbo:load`. When it changes (sign-out, a sign-in, a user switch, by Turbo
799
+ visit, or a `session:changed` from studio/session that is a real identity
800
+ change: `signed_out`, `changed`, or another viewer's identities, never the first
801
+ seating at load or a `stale` → `authenticated` revalidation), the browser drops
802
+ every cached fragment, every fetch in flight and every template already
803
+ inserted, so the next open fetches fresh. A modal open at that moment keeps its
804
+ template until it closes: a card is never emptied in place. The session id is
805
+ what `reset_session` rotates, and the CSRF secret rotates with it; the user id
806
+ catches a sign-in that keeps the session. The `csrf-token` meta is not used: it
807
+ is masked anew on every render, so it would empty the cache on every visit.
808
+
809
+ **Prefetch** runs when the browser idles after the first load and again after
810
+ every Turbo visit (`turbo:load`). An id already cached or in flight is not
811
+ fetched twice.
758
812
 
759
813
  **What does not change.** An id the registry does not name takes the old
760
814
  synchronous `open()` path, and an app that registers nothing renders the host
@@ -766,8 +820,8 @@ One difference for a registered id: during the first open's fetch,
766
820
  with no layout. It skips the host's `require_authentication` so an `:any`
767
821
  modal serves a signed-out visitor, and answers a `:user` modal's signed-out
768
822
  request with a bare `401`, never the `/login` redirect. Each answer is
769
- `private` with `Vary: Cookie` and an ETag. A fragment carries no script, so
770
- the page's CSP is unchanged. Only the shared host (`$store.modals`) loads on
823
+ `private` with `Vary: Cookie` and an ETag. A fragment carries no script (the
824
+ browser refuses one that does), so the page's CSP is unchanged. Only the shared host (`$store.modals`) loads on
771
825
  demand; a scoped host renders its modals as before.
772
826
 
773
827
  **Do not do both.** An id that the registry names and the layout still
@@ -1709,6 +1709,15 @@ body.modal-open .studio-app-banner {
1709
1709
  @apply px-8 py-3 text-base rounded-xl shadow-sm;
1710
1710
  }
1711
1711
 
1712
+ /* The medium button's label on the type scale: body size (1x --type-base), where a
1713
+ bare .btn keeps text-sm (14px, off the scale) so no host's hand-built button moves.
1714
+ Studio::ButtonComponent size: :md adds it. The 1.25 line height holds the bare
1715
+ .btn's 20px line, so the button grows no taller. Follows type-dense like type-body. */
1716
+ @utility btn-md {
1717
+ font-size: calc(var(--type-base, 16px) * 1);
1718
+ line-height: 1.25;
1719
+ }
1720
+
1712
1721
  /* A busy button (Studio::ButtonComponent loading:, or a submit Turbo marks busy) is
1713
1722
  disabled so it takes no second click, but it is working, not unavailable: it keeps
1714
1723
  its full colour and shows the progress cursor. The busy words sit in the button's
@@ -0,0 +1,56 @@
1
+ /*
2
+ studio-engine HOST ENTRY — what a host's one Tailwind line resolves to.
3
+
4
+ @import "tailwindcss";
5
+ @import "../builds/tailwind/studio_engine";
6
+
7
+ tailwindcss-rails' engines contract (Tailwindcss::Engines.bundle) writes
8
+ app/assets/builds/tailwind/studio_engine.css as an @import of the gem's
9
+ engine.css. Studio::TailwindEntry (lib/studio/tailwind_entry.rb) rewrites
10
+ that entry, right after the bundle runs, to import THIS file instead, so a
11
+ host gets, from the same line and with nothing of its own:
12
+
13
+ - engine.css: the theme (theme.css, the tokens that replaced the JS
14
+ preset and the host's `@config`) and the component @utility layer;
15
+ - the @source scan below, so no host lists the gem in a content glob or
16
+ safelists its classes;
17
+ - engine-motion.css, the motion / effect / leveling layer, which used to
18
+ need a per-host rake task (lib/tasks/tailwindcss_engine_motion.rake).
19
+
20
+ The path is resolved by Ruby from the gem's root, so Node never runs bundler
21
+ to find the gem. engine.css stays importable on its own as the narrow layer.
22
+ Recipe: README, "Tailwind".
23
+ */
24
+ @import "./engine.css";
25
+
26
+ /* -- Sources: every engine directory that emits a class --------------------
27
+ Tailwind v4 compiles only the classes it SEES, so every directory whose
28
+ files put a utility class on the page is scanned here, relative to this
29
+ file, in every host that imports it, with no host edit: views and
30
+ partials, the ViewComponents (their colour classes live in Ruby), the
31
+ helpers that build markup (admin_model_table), and the engine's ES modules
32
+ (app/javascript: classes added at runtime). app/assets/javascripts is not
33
+ scanned: since the engine's scripts became modules it holds the vendored,
34
+ minified third-party builds (Alpine, Leaflet, Sortable, canvas-confetti) and
35
+ studio/session.js, which puts no class on a page (held by
36
+ test/integration/component_tailwind_source_test.rb).
37
+
38
+ The exclusions are NARROW on purpose: the vendored bundles are not engine
39
+ markup, and scanning them mints junk utilities from their identifiers. Never exclude a whole directory: the hub's
40
+ `@source not "../../../vendor/bundle"` once dropped every one of these
41
+ lines in CI, because the gem itself lives under vendor/bundle there.
42
+
43
+ test/integration/component_tailwind_source_test.rb proves every class these
44
+ directories emit compiles, and that dropping any one line turns it red. A
45
+ class built by interpolation (`bg-<%= tone %>`) is invisible to the scan;
46
+ test/views/dynamic_tailwind_class_guard_test.rb refuses one. */
47
+ @source "../../../views";
48
+
49
+ @source "../../../helpers";
50
+ @source "../../../javascript";
51
+ @source not "../../../javascript/studio/vendor";
52
+
53
+ /* -- Motion / effect / leveling layer ---------------------------------------
54
+ Last, so the theme and component layer it reads are in scope, as when hosts
55
+ imported it after engine.css themselves. */
56
+ @import "./engine-motion.css";
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Studio
4
- # Every state Studio::ButtonComponent can show: the four tones, the three
4
+ # Every state Studio::ButtonComponent can show: the five tones, the three
5
5
  # sizes, a link, an icon, disabled and loading. No @param tags: the gallery
6
6
  # takes no input.
7
7
  class ButtonComponentPreview < ViewComponent::Preview
8
- # The four tones, at the default size.
8
+ # The five tones, at the default size.
9
9
  def tones
10
10
  render_with_template(locals: { tones: Studio::ButtonComponent::TONES.keys })
11
11
  end
@@ -10,5 +10,5 @@
10
10
  %>
11
11
  <div class="card overflow-hidden" data-specimen="table">
12
12
  <%= render Studio::TableComponent.new(rows: rows, columns: columns, sort: sort, direction: direction,
13
- sort_url: ->(key, dir) { "?sort=#{key}&direction=#{dir}" }, empty: "No members yet.", min_width: 520) %>
13
+ sort_url: ->(key, dir) { "?sort=#{key}&direction=#{dir}" }, empty: "No members yet.", min_width: 520, caption: "Team members") %>
14
14
  </div>
@@ -6,6 +6,7 @@ module Studio
6
6
  # render Studio::ButtonComponent.new(label: "Save")
7
7
  # render Studio::ButtonComponent.new(label: "Delete", tone: :danger, size: :sm)
8
8
  # render Studio::ButtonComponent.new(label: "Docs", tone: :quiet, href: "/docs")
9
+ # render Studio::ButtonComponent.new(label: "Export", tone: :outline)
9
10
  # render Studio::ButtonComponent.new(label: "Save", type: "submit", loading: true, busy_label: "Saving…")
10
11
  # render(Studio::ButtonComponent.new(label: "New")) { |b| b.with_icon { plus_svg } }
11
12
  #
@@ -18,7 +19,12 @@ module Studio
18
19
  # secondary btn-secondary the success role, or the app's retune
19
20
  # quiet btn-neutral a second action: Cancel, Back, Next
20
21
  # danger btn-danger a destructive action
21
- # size sm btn-sm · md btn · lg btn-lg
22
+ # outline btn-outline a bordered, unfilled action on the
23
+ # primary colour; it fills on hover
24
+ # size sm btn-sm · md btn-md · lg btn-lg
25
+ #
26
+ # md's label sits on the type scale at body size (btn-md, 1x --type-base); a
27
+ # bare .btn keeps text-sm, so a host's hand-built buttons do not move.
22
28
  #
23
29
  # Its public surface, which a release changes only with a Breaking line in the
24
30
  # changelog (docs/FRONT_END_STANDARD.md, "A primitive's public surface"):
@@ -36,7 +42,9 @@ module Studio
36
42
  # always carries the pending loader, hidden, so a Turbo form that marks itself
37
43
  # aria-busy shows it with no script here. The icon is the idle content and
38
44
  # gives way to the loader; busy_label: words replace the label while busy.
39
- # A link cannot be busy, so loading: with href: is refused.
45
+ # A link cannot be busy, so loading: with href: is refused. A busy FORM hides
46
+ # the icon on every button inside it, not just the submit (engine-motion.css,
47
+ # :where(form, button)[aria-busy] [data-loader-idle]); README, "UI Primitives".
40
48
  #
41
49
  # Every class string is written out whole: Tailwind finds utilities by scanning
42
50
  # source text, and engine.css adds app/components to every host's scan.
@@ -53,12 +61,13 @@ module Studio
53
61
  primary: "btn-primary",
54
62
  secondary: "btn-secondary",
55
63
  quiet: "btn-neutral",
56
- danger: "btn-danger"
64
+ danger: "btn-danger",
65
+ outline: "btn-outline"
57
66
  }.freeze
58
67
 
59
68
  SIZES = {
60
69
  sm: "btn-sm",
61
- md: nil,
70
+ md: "btn-md",
62
71
  lg: "btn-lg"
63
72
  }.freeze
64
73
 
@@ -1,5 +1,6 @@
1
- <div class="<%= Studio::TableComponent::WRAPPER %>" data-table>
1
+ <%= tag.div(**wrapper_attributes) do %>
2
2
  <%= tag.table(**table_attributes) do %>
3
+ <% if caption %><caption class="<%= Studio::TableComponent::CAPTION %>"><%= caption %></caption><% end %>
3
4
  <thead class="<%= Studio::TableComponent::HEAD %>">
4
5
  <tr>
5
6
  <% columns.each do |column| %>
@@ -29,4 +30,4 @@
29
30
  <% end %>
30
31
  </tbody>
31
32
  <% end %>
32
- </div>
33
+ <% end %>
@@ -12,7 +12,8 @@ module Studio
12
12
  # { key: :created_at, label: "Joined", align: :right, cell: ->(user) { l(user.created_at.to_date) } }
13
13
  # ],
14
14
  # sort: params[:sort], direction: params[:direction],
15
- # empty: "No users yet."
15
+ # empty: "No users yet.",
16
+ # caption: "Team members"
16
17
  # )
17
18
  #
18
19
  # A column is a Hash: key: (required), label: (required), sortable: (false),
@@ -32,21 +33,36 @@ module Studio
32
33
  # (admin_model_table, the emails index): the table keeps its columns and its
33
34
  # wrapper scrolls. min_width: sets the width under which it scrolls.
34
35
  #
36
+ # A NAME AND A KEYBOARD SCROLL. The scroll wrapper takes tabindex="0", so a
37
+ # keyboard user can focus it and scroll it with the arrow keys (WCAG 2.1.1; axe's
38
+ # scrollable-region-focusable), and it shows a focus ring inside its edge, so a
39
+ # card's overflow-hidden cannot clip it.
40
+ # caption: renders a <caption> (visually hidden: the page's own heading is the
41
+ # visible title) and aria_label: labels the table without one. Either one also
42
+ # makes the wrapper a named role="region", so a screen reader announces what it
43
+ # entered. With neither, the wrapper stays focusable but takes no role: a
44
+ # region with no name is noise. Pass one.
45
+ #
35
46
  # Its public surface, which a release changes only with a Breaking line in the
36
47
  # changelog (docs/FRONT_END_STANDARD.md, "A primitive's public surface"):
37
48
  # inputs rows:, columns:, id:, sort:, direction:, sort_url:, sort_param:,
38
- # direction_param:, empty:, min_width:, sticky_header:
49
+ # direction_param:, empty:, min_width:, sticky_header:,
50
+ # caption:, aria_label:
39
51
  # data-* data-table on the wrapper; data-table-sort-key on a sortable
40
52
  # header; data-table-empty on the empty row; data-sticky-table-header
41
53
  # on the table when sticky_header: (studio/sticky_table_header)
42
- # aria-* aria-sort on the sorted header
54
+ # aria-* aria-sort on the sorted header; aria-label on the table
55
+ # (aria_label:) and on the wrapper, role="region" (caption: or
56
+ # aria_label:)
57
+ # tabindex tabindex="0" on the wrapper
43
58
  #
44
59
  # Every class string is written out whole: Tailwind finds utilities by scanning
45
60
  # source text, and engine.css adds app/components to every host's scan.
46
61
  class TableComponent < ViewComponent::Base
47
62
  strip_trailing_whitespace
48
63
 
49
- WRAPPER = "overflow-x-auto"
64
+ WRAPPER = "overflow-x-auto focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-primary"
65
+ CAPTION = "sr-only"
50
66
  TABLE = "w-full type-body"
51
67
  HEAD = "bg-surface-alt text-muted type-fine uppercase tracking-wide"
52
68
  BODY = "divide-y divide-subtle"
@@ -71,7 +87,8 @@ module Studio
71
87
  attr_reader :rows, :columns, :empty
72
88
 
73
89
  def initialize(rows:, columns:, id: nil, sort: nil, direction: nil, sort_url: nil, sort_param: "sort",
74
- direction_param: "direction", empty: "Nothing here yet.", min_width: nil, sticky_header: false)
90
+ direction_param: "direction", empty: "Nothing here yet.", min_width: nil, sticky_header: false,
91
+ caption: nil, aria_label: nil)
75
92
  super()
76
93
  raise ArgumentError, "Studio::TableComponent needs at least one column" if columns.blank?
77
94
 
@@ -86,12 +103,29 @@ module Studio
86
103
  @empty = empty
87
104
  @min_width = min_width
88
105
  @sticky_header = sticky_header ? true : false
106
+ @caption = caption.presence
107
+ @aria_label = aria_label.presence
108
+ end
109
+
110
+ attr_reader :caption
111
+
112
+ # The name the wrapper's region announces: the caption, else aria_label:.
113
+ def accessible_name = @caption || @aria_label
114
+
115
+ def wrapper_attributes
116
+ attrs = { class: WRAPPER, tabindex: "0", data: { table: "" } }
117
+ if accessible_name
118
+ attrs[:role] = "region"
119
+ attrs[:aria] = { label: accessible_name }
120
+ end
121
+ attrs
89
122
  end
90
123
 
91
124
  def table_attributes
92
125
  attrs = { class: TABLE, id: @id }
93
126
  attrs[:style] = "min-width: #{@min_width.to_i}px" if @min_width
94
127
  attrs[:data] = { sticky_table_header: "" } if @sticky_header
128
+ attrs[:aria] = { label: @aria_label } if @aria_label && !@caption
95
129
  attrs.compact
96
130
  end
97
131
 
@@ -22,7 +22,14 @@ module Studio
22
22
  # so it is private: Rails' default for a dynamic GET (max-age=0, private,
23
23
  # must-revalidate, with an ETag) is kept, and Vary: Cookie is added so no
24
24
  # cache answers one session with another's fragment. The browser holds the
25
- # fragment for the page's lifetime anyway (studio/modal_host).
25
+ # fragment across Turbo visits and drops it when the host's session marker
26
+ # changes (Studio::Modals.session_marker, studio/modal_host).
27
+ #
28
+ # NO SCRIPT. studio/modal_host refuses a fragment that carries a <script>.
29
+ # The browser says nothing in production, so the refusal is recorded here,
30
+ # as an ErrorLog row, where it is seen. The fragment is still sent: the
31
+ # browser's refusal (with its reason on the card in development) is the
32
+ # behaviour; this is the record.
26
33
  class ModalsController < ::ApplicationController
27
34
  skip_before_action :require_authentication, raise: false
28
35
 
@@ -37,10 +44,34 @@ module Studio
37
44
  # the host's error page for the browser to mistake for a fragment.
38
45
  response.headers["X-Studio-Modal"] = entry.id
39
46
  response.headers["Vary"] = [response.headers["Vary"], "Cookie"].compact.join(", ")
47
+ record_script_fragment(entry) if response.body.to_s.match?(SCRIPT_TAG)
40
48
  end
41
49
 
42
50
  private
43
51
 
52
+ SCRIPT_TAG = %r{<script[\s>/]}i
53
+
54
+ # Ids already recorded in this process: one row per bad modal, not one per
55
+ # request (a prefetched bad fragment would otherwise write a row on every
56
+ # page view).
57
+ RECORDED_SCRIPT_IDS = Set.new
58
+ RECORDED_SCRIPT_LOCK = Mutex.new
59
+
60
+ def self.forget_recorded_scripts! = RECORDED_SCRIPT_LOCK.synchronize { RECORDED_SCRIPT_IDS.clear }
61
+
62
+ # A best-effort record: logging never breaks the answer.
63
+ def record_script_fragment(entry)
64
+ return unless defined?(::ErrorLog) && ::ErrorLog.respond_to?(:capture!)
65
+ return unless RECORDED_SCRIPT_LOCK.synchronize { RECORDED_SCRIPT_IDS.add?(entry.id) }
66
+
67
+ error = RuntimeError.new("Studio.modals #{entry.id}: #{entry.partial} renders a <script>; " \
68
+ "studio/modal_host refuses it. Move the behaviour to a module or a Stimulus controller.")
69
+ error.set_backtrace(caller)
70
+ ::ErrorLog.capture!(error)
71
+ rescue StandardError
72
+ nil
73
+ end
74
+
44
75
  def studio_modal_viewer_signed_in?
45
76
  respond_to?(:logged_in?, true) && logged_in?
46
77
  end