Skip to content

Commit 6dc0187

Browse files
committed
wip: draw action pressed on the record header and the declared actions bar
1 parent 6285203 commit 6dc0187

5 files changed

Lines changed: 183 additions & 7 deletions

File tree

‎ui/packages/app-shell/src/views/DeclaredActionsBar.tsx‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,28 @@ function useDeclaredActionShown(action: ActionDef, predicateRecord: ReturnType<t
215215
return !(hasDeclaredVisibilityGate(action.visible) && !isVisible);
216216
}
217217

218+
/**
219+
* The reader's own state (`ActionSchema.pressed`, objectstack#27946): does the
220+
* action DECLARE a pressed predicate, and does it hold for this record?
221+
*
222+
* `undefined` = no predicate, so the lever is a plain button and carries no
223+
* `aria-pressed`; otherwise the verdict, drawn as `aria-pressed` and the
224+
* pressed face. Same entry and record binding as `visible` above, with
225+
* `throwOnError` so a predicate that cannot be evaluated answers `false` — the
226+
* plain button, never a claim that the reader acted — and is reported once. A
227+
* boolean is refused by the spec at parse (a constant is a style, not a
228+
* reader's state), so it gets no reading here. Display only: the verdict never
229+
* gates, greys or routes the action.
230+
*/
231+
function useDeclaredActionPressed(action: ActionDef, predicateRecord: ReturnType<typeof usePredicateRecordContext>): boolean | undefined {
232+
const declared = typeof action.pressed !== 'boolean' && hasDeclaredVisibilityGate(action.pressed);
233+
const isPressed = useCondition(declared ? toPredicateInput(action.pressed) : undefined, predicateRecord, {
234+
throwOnError: true,
235+
label: `declared action "${action.name ?? action.label ?? 'action'}" (pressed)`,
236+
});
237+
return declared ? isPressed : undefined;
238+
}
239+
218240
/**
219241
* Report whether a declared action is drawn, and withdraw the report when the
220242
* reporter unmounts (objectstack#25877). `onDrawn` is the toolbar's stable
@@ -327,6 +349,7 @@ const DeclaredActionButton: React.FC<{
327349
// these two lines carried — which existed only because `ActionDef.disabled`
328350
// could not describe the envelope arm — have nothing left to reach around.
329351
const isDisabledPred = useCondition(toPredicateInput(action.disabled), predicateRecord);
352+
const pressed = useDeclaredActionPressed(action, predicateRecord);
330353
// Called up here with the other hooks: the `visible` gate below returns early.
331354
const reasonId = React.useId();
332355
const labelId = React.useId();
@@ -651,13 +674,19 @@ const DeclaredActionButton: React.FC<{
651674
variant={variant as any}
652675
disabled={disabledByPredicate || loading}
653676
aria-describedby={disabledReason ? reasonId : undefined}
677+
aria-pressed={pressed}
654678
onClick={handleClick}
655679
// Amber warning treatment for an override (objectui#5178) — the same
656680
// palette the approvals surfaces already use for "waiting / attention",
657681
// via `cn()` over the outline variant rather than a new Button variant
658682
// (`packages/components/src/ui/**` is a no-touch Shadcn zone).
659-
className={cn(isOverride
660-
&& 'border-amber-500/60 text-amber-700 hover:bg-amber-50 hover:text-amber-800 dark:text-amber-400 dark:hover:bg-amber-500/10 dark:hover:text-amber-300')}
683+
// The pressed face is Shadcn's toggle-on colours keyed off the attribute,
684+
// the record header's spelling (`PageHeaderActions`).
685+
className={cn(
686+
isOverride
687+
&& 'border-amber-500/60 text-amber-700 hover:bg-amber-50 hover:text-amber-800 dark:text-amber-400 dark:hover:bg-amber-500/10 dark:hover:text-amber-300',
688+
pressed !== undefined && 'aria-pressed:bg-accent aria-pressed:text-accent-foreground hover:aria-pressed:bg-accent',
689+
)}
661690
data-testid={`declared-action-${action.name}`}
662691
// Pinnable, and readable by an E2E/QA pass without matching on copy.
663692
data-override-decision={isOverride ? 'true' : undefined}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* objectstack#28242 — a `page:header` action that declares `pressed` (the
3+
* reader's own state, `ActionSchema.pressed`) is a toggle button: drawn with
4+
* `aria-pressed="true"` and the pressed face while the predicate holds for
5+
* this record, `aria-pressed="false"` while it does not. An action that
6+
* declares no predicate carries no `aria-pressed` at all.
7+
*
8+
* Display only: a pressed action stays enabled and still dispatches, and a
9+
* predicate that cannot be evaluated draws the plain button (never a claim
10+
* that the reader acted) and says so once.
11+
*/
12+
13+
import * as React from 'react';
14+
import { describe, it, expect, vi, afterEach } from 'vitest';
15+
import { render, screen, waitFor } from '@testing-library/react';
16+
import userEvent from '@testing-library/user-event';
17+
import '@testing-library/jest-dom';
18+
import { ComponentRegistry } from '@object-ui/core';
19+
import { ActionProvider, RecordContextProvider } from '@object-ui/react';
20+
// Registers `page:header` at module scope (object-ui/no-dynamic-import-in-test-hook).
21+
import '../renderers';
22+
23+
const HELPFUL = {
24+
name: 'mark_article_helpful',
25+
label: 'Helpful',
26+
type: 'script',
27+
locations: ['record_header'],
28+
pressed: "has(record.viewer) && record.viewer.vote == 'helpful'",
29+
};
30+
const NOT_HELPFUL = {
31+
name: 'mark_article_not_helpful',
32+
label: 'Not helpful',
33+
type: 'script',
34+
locations: ['record_header'],
35+
pressed: { dialect: 'cel', source: "has(record.viewer) && record.viewer.vote == 'not_helpful'" },
36+
};
37+
const SHARE = { name: 'share_article', label: 'Share', type: 'script', locations: ['record_header'] };
38+
39+
function PageHeader({ schema }: { schema: any }) {
40+
const Component = ComponentRegistry.get('page:header');
41+
if (!Component) throw new Error('page:header not registered');
42+
// eslint-disable-next-line react-hooks/static-components -- ComponentRegistry.get returns a registered component (stable), not one created during render
43+
return <Component schema={schema} />;
44+
}
45+
46+
function mount(actions: Record<string, unknown>[], record: Record<string, unknown>, handler?: (a: any) => any) {
47+
return render(
48+
<ActionProvider handlers={handler ? { 'vote-run': handler } : undefined}>
49+
<RecordContextProvider
50+
objectName="kb_article"
51+
recordId="a-1"
52+
data={{ id: 'a-1', ...record }}
53+
objectSchema={{ name: 'kb_article', label: 'Article' }}
54+
>
55+
<PageHeader schema={{ type: 'page:header', title: 'Article', maxVisible: 5, actions }} />
56+
</RecordContextProvider>
57+
</ActionProvider>,
58+
);
59+
}
60+
61+
afterEach(() => vi.restoreAllMocks());
62+
63+
describe('page:header — `pressed` draws the reader\'s own state (objectstack#28242)', () => {
64+
it('the predicate holds: aria-pressed="true" and the pressed face; the sibling vote is unpressed', () => {
65+
mount([HELPFUL, NOT_HELPFUL], { viewer: { vote: 'helpful' } });
66+
const helpful = screen.getByRole('button', { name: 'Helpful' });
67+
expect(helpful).toHaveAttribute('aria-pressed', 'true');
68+
expect(helpful.className).toContain('aria-pressed:bg-accent');
69+
expect(screen.getByRole('button', { name: 'Not helpful' })).toHaveAttribute('aria-pressed', 'false');
70+
});
71+
72+
it('the envelope arm reads the same: the other vote is the pressed one', () => {
73+
mount([HELPFUL, NOT_HELPFUL], { viewer: { vote: 'not_helpful' } });
74+
expect(screen.getByRole('button', { name: 'Helpful' })).toHaveAttribute('aria-pressed', 'false');
75+
expect(screen.getByRole('button', { name: 'Not helpful' })).toHaveAttribute('aria-pressed', 'true');
76+
});
77+
78+
it('a row with no served block (fetched by another route) draws both unpressed', () => {
79+
mount([HELPFUL, NOT_HELPFUL], {});
80+
expect(screen.getByRole('button', { name: 'Helpful' })).toHaveAttribute('aria-pressed', 'false');
81+
expect(screen.getByRole('button', { name: 'Not helpful' })).toHaveAttribute('aria-pressed', 'false');
82+
});
83+
84+
it('display only: a pressed action stays enabled and still dispatches', async () => {
85+
const user = userEvent.setup();
86+
const handler = vi.fn(async () => ({ success: true }));
87+
mount([{ ...HELPFUL, type: 'vote-run' }], { viewer: { vote: 'helpful' } }, handler);
88+
const helpful = screen.getByRole('button', { name: 'Helpful' });
89+
expect(helpful).not.toBeDisabled();
90+
await user.click(helpful);
91+
await waitFor(() => expect(handler).toHaveBeenCalledTimes(1));
92+
expect((handler.mock.calls[0] as unknown[])[0]).toMatchObject({ name: 'mark_article_helpful' });
93+
});
94+
95+
it('a predicate that cannot be evaluated draws the plain button, and says so', () => {
96+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
97+
mount([{ ...HELPFUL, pressed: "record.viewer.vote == 'helpful'" }], {});
98+
expect(screen.getByRole('button', { name: 'Helpful' })).toHaveAttribute('aria-pressed', 'false');
99+
expect(warn.mock.calls.some((c) => String(c[0]).includes('pressed'))).toBe(true);
100+
});
101+
102+
it('control — an action that declares no predicate is a plain button: no aria-pressed, no face', () => {
103+
mount([SHARE], { viewer: { vote: 'helpful' } });
104+
const share = screen.getByRole('button', { name: 'Share' });
105+
expect(share).not.toHaveAttribute('aria-pressed');
106+
expect(share.className).not.toContain('aria-pressed:');
107+
});
108+
});

‎ui/packages/components/src/renderers/layout/containers.tsx‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,22 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
24042404
if (!src) return false;
24052405
return evalHeaderPredicate(d, `page:header action "${String(actionName)}" disabled`, true);
24062406
};
2407+
// The reader's own state (`ActionSchema.pressed`, objectstack#27946): a
2408+
// DECLARED predicate makes the button a toggle, drawn pressed while it is
2409+
// TRUE. Same entry and bindings as `visible` / `disabled`; its fail
2410+
// direction is "not pressed" (`fallback: false`), said once — a fault
2411+
// draws the plain button and never claims the reader acted. Display only:
2412+
// nothing here gates, greys or routes on it. `undefined` = no predicate
2413+
// (not a toggle); a boolean is refused by the spec at parse, so it gets no
2414+
// reading here.
2415+
const pressedFor = (action: any): boolean | undefined => {
2416+
const p = action?.pressed;
2417+
const src = typeof p === 'string'
2418+
? p
2419+
: (p && typeof p === 'object' && typeof (p as any).source === 'string' ? (p as any).source : undefined);
2420+
if (!src || src.trim() === '') return undefined;
2421+
return evalHeaderPredicate(p, `page:header action "${String(action?.name ?? action?.id)}" pressed`, false);
2422+
};
24072423
// A live inline-edit session disables actions the host flagged with
24082424
// `disableDuringInlineEdit` (objectui#2572 item 4) — see `inlineEditing`
24092425
// at the top of the renderer.
@@ -2442,14 +2458,22 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
24422458
const actionKey = action.name || action.id || `header-action-${idx}`;
24432459
const disabledReason = disabledReasonFor(action);
24442460
const reasonId = `${disabledReasonIdBase}-reason-${idx}`;
2461+
const pressed = pressedFor(action);
24452462
const button = (
24462463
<Button
24472464
key={actionKey}
24482465
variant={variant}
24492466
size={size}
24502467
disabled={disabled}
24512468
aria-describedby={disabledReason ? reasonId : undefined}
2452-
className="gap-2"
2469+
aria-pressed={pressed}
2470+
// The pressed face: Shadcn's toggle-on colours, keyed off the
2471+
// attribute so the face and `aria-pressed` cannot disagree (the
2472+
// same spelling as `PanelToggles` and `DeclaredActionsBar`).
2473+
className={cn(
2474+
'gap-2',
2475+
pressed !== undefined && 'aria-pressed:bg-accent aria-pressed:text-accent-foreground hover:aria-pressed:bg-accent',
2476+
)}
24532477
onClick={() => {
24542478
if (typeof action.onClick === 'function') {
24552479
void action.onClick();

‎ui/packages/core/src/actions/ActionRunner.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ export interface ActionDef {
196196
* so the two keys cannot drift apart again.
197197
*/
198198
disabled?: SpecActionInput['disabled'];
199+
/**
200+
* Pressed-state predicate — the button is drawn already pressed for this
201+
* reader (`aria-pressed="true"` and the pressed face) while it holds.
202+
*
203+
* Derived from the spec field. Unlike {@link ActionDef.disabled} it has no
204+
* boolean arm: the spec refuses a literal, because a constant is a style and
205+
* not the reader's state. Display only — the runner never reads it, and a
206+
* pressed action stays runnable. Drawn by the record header
207+
* (`PageHeaderActions`) and the declared actions bar
208+
* (`DeclaredActionButton`); a predicate that cannot be evaluated draws the
209+
* plain, unpressed button.
210+
*/
211+
pressed?: SpecActionInput['pressed'];
199212
/**
200213
* API endpoint (string URL or complex config).
201214
*

‎ui/packages/core/src/actions/actionKeys.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ export const ACTION_DEF_KEYS = [
128128
'confirm',
129129
'condition',
130130
'disabled',
131+
// The pressed-state predicate (`ActionSchema.pressed`), derived from the
132+
// spec and drawn as `aria-pressed` by the record header and the declared
133+
// actions bar. Display only: the runner never reads it.
134+
'pressed',
131135
'api',
132136
'endpoint',
133137
'method',
@@ -301,10 +305,8 @@ export const SPEC_ACTION_KEYS = [
301305
'patch',
302306
// Added to `ActionSchema` by objectstack#27946: the pressed-state predicate a
303307
// header button draws as `aria-pressed` for the reader ("you voted Helpful").
304-
// Listed for the reason `description` is — this array restates the spec's
305-
// declared keys, so an action carrying it is not reported as having an
306-
// unknown key. `ActionDef` does NOT declare it and no surface reads it yet:
307-
// the console card that draws the pressed face is a follow-up owed.
308+
// `ActionDef` declares it (derived), and the record header and the declared
309+
// actions bar draw it.
308310
'pressed',
309311
'recordIdField',
310312
'recordIdParam',

0 commit comments

Comments
 (0)