Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/alerts-clear-of-controls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@swirls/spools': patch
---

Alerts are drawn in the top right corner of the page, under the top bar, and are narrower.
At the bottom of the page an alert that stays (`danger`, `working`, `stays`) sat over a
form's buttons, a panel pinned to the bottom and the last rows of a list until it was
dismissed.
4 changes: 2 additions & 2 deletions apps/docs/src/docs-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@
{
"path": "/docs/alerts",
"title": "Alerts",
"description": "Alerts are how the CRM tells whoever is using it what is happening: a save the server refused, an action that finished, work that is taking a while. They are drawn in one place, over the bottom of the page, by the CRM's shell. The CRM's own parts raise them, and so can your layouts, your components and your modules.",
"description": "Alerts are how the CRM tells whoever is using it what is happening: a save the server refused, an action that finished, work that is taking a while. They are drawn in one place by the CRM's shell: the top right corner of the page, under the top bar. The CRM's own parts raise them, and so can your layouts, your components and your modules.",
"sections": [
{
"id": "what-the-crm-raises-on-its-own",
Expand Down Expand Up @@ -751,7 +751,7 @@
"level": 2
}
],
"text": "Alerts Alerts are how the CRM tells whoever is using it what is happening: a save the server refused, an action that finished, work that is taking a while. They are drawn in one place, over the bottom of the page, by the CRM's shell. The CRM's own parts raise them, and so can your layouts, your components and your modules. What the CRM raises on its own When What is said A field changed in place is refused by the server \"Value was not saved\", and what the server said A step of Stages is refused \"Stage was not changed\", and what the server said A record action ( actions in the config) runs for a while The action's label, with a spinner A record action ends The action's message, or why it failed A save that works says nothing: the new value on the page is the news. From a layout @Alert(text, tone?) is an action, beside @Navigate and @Refresh: The steps of an Action run in order and stop at a Mutation that fails, so an alert after @Run is raised only when the Mutation worked. tone is info (when left out), success, warning or danger. From a component useAlerts() works in any component drawn inside the CRM: a layout component ( defineLayoutComponent), a module's panel, a page of your own under src/routes/_crm. Call What it does alerts.show(alert) Raises an alert and gives its id. alerts.change(id, alert) Replaces an alert that is up. alerts.dismiss(id) Takes one down. alerts.during(work, says) Says doing if the promise is still going after a moment, then done or failed. Gives the promise's own result, and fails when it does. An alert is { text, tone?, detail?, link?, stays? }: tone: info, success, warning, danger, or working (a spinner). detail: a second line, such as why it failed. link: { label, to }, somewhere in the CRM to go from here. stays: keeps it up until it is dismissed. danger and working alerts always stay; the rest go after five seconds. during leaves out done to say nothing when the work succeeds, and waits a moment before announcing work, so something quick never flashes a spinner. At most four alerts are up at once: the oldest that would have gone on its own makes room. Outside the CRM's shell (a public page, a unit test) useAlerts() draws nothing and during still runs the work. Hearing every alert onAlert in defineSpoolUi is called with every alert as it is raised or changed, the CRM's own included. Use it to log them or to send them somewhere of your own. What alerts are not Alerts are for the person at the page, about what they just did. They are not kept, and nobody else sees them. To tell a teammate something later, make a task or use an automation (see configuration.md)."
"text": "Alerts Alerts are how the CRM tells whoever is using it what is happening: a save the server refused, an action that finished, work that is taking a while. They are drawn in one place by the CRM's shell: the top right corner of the page, under the top bar. The CRM's own parts raise them, and so can your layouts, your components and your modules. That corner keeps alerts off the foot of the page: a form's buttons, a panel pinned to the bottom and the last rows of a list stay within reach while an alert is up, one that stays until it is dismissed included. What an alert can be over is the right end of whatever is directly under the top bar (a list's search and its \"New\" button, the last tabs of a record), until it goes or is dismissed. An alert raised while a dialog is open is drawn behind it, and is there when the dialog closes: say what a dialog's own form got wrong in the dialog. What the CRM raises on its own When What is said A field changed in place is refused by the server \"Value was not saved\", and what the server said A step of Stages is refused \"Stage was not changed\", and what the server said A record action ( actions in the config) runs for a while The action's label, with a spinner A record action ends The action's message, or why it failed A save that works says nothing: the new value on the page is the news. From a layout @Alert(text, tone?) is an action, beside @Navigate and @Refresh: The steps of an Action run in order and stop at a Mutation that fails, so an alert after @Run is raised only when the Mutation worked. tone is info (when left out), success, warning or danger. From a component useAlerts() works in any component drawn inside the CRM: a layout component ( defineLayoutComponent), a module's panel, a page of your own under src/routes/_crm. Call What it does alerts.show(alert) Raises an alert and gives its id. alerts.change(id, alert) Replaces an alert that is up. alerts.dismiss(id) Takes one down. alerts.during(work, says) Says doing if the promise is still going after a moment, then done or failed. Gives the promise's own result, and fails when it does. An alert is { text, tone?, detail?, link?, stays? }: tone: info, success, warning, danger, or working (a spinner). detail: a second line, such as why it failed. link: { label, to }, somewhere in the CRM to go from here. stays: keeps it up until it is dismissed. danger and working alerts always stay; the rest go after five seconds. during leaves out done to say nothing when the work succeeds, and waits a moment before announcing work, so something quick never flashes a spinner. At most four alerts are up at once: the oldest that would have gone on its own makes room. Outside the CRM's shell (a public page, a unit test) useAlerts() draws nothing and during still runs the work. Hearing every alert onAlert in defineSpoolUi is called with every alert as it is raised or changed, the CRM's own included. Use it to log them or to send them somewhere of your own. What alerts are not Alerts are for the person at the page, about what they just did. They are not kept, and nobody else sees them. To tell a teammate something later, make a task or use an automation (see configuration.md)."
},
{
"path": "/docs/integrations",
Expand Down
2 changes: 1 addition & 1 deletion apps/starter/e2e/layouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import { TAGGER } from './sections'

export const PIPELINE_PAGE = [
'root = Page([stats, Card([Funnel(byStage.groups, "deals", "currency", ["closeDate"])], "By stage"), Row([Input("q", "Search deals", "search", $q), NewRecord("deals")], "between"), Card([Records("deals", deals.rows, ["value", "stage", "closeDate"], "No deals match.")], "Deals")], "wide")',
'root = Page([stats, Card([Funnel(byStage.groups, "deals", "currency", ["closeDate"])], "By stage"), Row([Input("q", "Search deals", "search", $q), NewRecord("deals")], "between"), Card([Records("deals", deals.rows, ["value", "stage", "closeDate"], "No deals match.")], "Deals"), SignOff()], "wide")',
'$q = ""',
'deals = Query("records", {object: "deals", q: $q, sort: {field: "closeDate", dir: "asc"}}, {rows: [], total: 0})',
'won = Query("records", {object: "deals", where: {stage: "won"}, limit: 1}, {rows: [], total: 0})',
Expand Down
45 changes: 44 additions & 1 deletion apps/starter/e2e/spool.ui.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState } from 'react'
import { z } from 'zod'

import { devAuthClient } from '@swirls/spools/auth/dev/ui'
Expand Down Expand Up @@ -41,6 +42,48 @@ const Nudge = defineLayoutComponent({
},
})

/**
* A component of the app's own that stays at the bottom of the page, as a panel with a form's
* button in it does, and whose every press is refused: the alert saying so stays up, and the
* button has to be reachable all the same (e2e/tests/alerts.e2e.ts).
*/
const SignOff = defineLayoutComponent({
name: 'SignOff',
description: 'A button at the bottom of the page that is refused, and says so.',
props: z.object({}),
Component: () => {
const alerts = useAlerts()
const [tries, setTries] = useState(0)

const signOff = () => {
setTries((now) => now + 1)
alerts.show({ tone: 'danger', text: 'Not signed off', detail: 'A deal is still open.' })
}

return (
<div
style={{
position: 'sticky',
bottom: 0,
display: 'flex',
justifyContent: 'center',
pointerEvents: 'none',
}}
>
<button
type="button"
data-own="sign-off"
data-tries={tries}
style={{ pointerEvents: 'auto' }}
onClick={signOff}
>
Sign off
</button>
</div>
)
},
})

/**
* The example Approvals module's panels and sidebar link. One build serves
* every server of the browser tests, so it is here for all of them; it shows
Expand All @@ -49,7 +92,7 @@ const Nudge = defineLayoutComponent({
*/
export const ui = defineSpoolUi({
modules: [approvalsUi],
components: [DealCount, Nudge],
components: [DealCount, Nudge, SignOff],
// Every alert the CRM raises is heard here: the tests read what was (e2e/tests/alerts.e2e.ts).
onAlert: (alert) => {
const heard = document.documentElement.dataset['alertsHeard']
Expand Down
29 changes: 29 additions & 0 deletions apps/starter/e2e/tests/alerts.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,32 @@ test('a field the server will not save says so, by name, and stays until dismiss
await alert.getByRole('button', { name: 'Dismiss' }).click()
await expect(alert).toHaveCount(0)
})

test('an alert that stays leaves a button at the bottom of the page within reach', async ({
page,
}) => {
// Shorter than the page, so the button is held at the foot of the window, as a pinned panel's is.
const height = 560

await page.setViewportSize({ width: 1280, height })
await page.goto('/pages/pipeline')

const signOff = page.locator('[data-own="sign-off"]')
const box = await signOff.boundingBox()

expect(box && height - (box.y + box.height)).toBeLessThan(80)

await signOff.click()

const alert = alerts(page).getByRole('alert')

await expect(alert).toContainText('Not signed off')

// Pressed again with the alert still up: nothing of the alert is over it.
await signOff.click({ timeout: 3000 })
await expect(signOff).toHaveAttribute('data-tries', '2')
await expect(alert).toHaveCount(2)

// Nor over the last of the list above it.
await page.getByRole('main').getByRole('link').last().click({ timeout: 3000, trial: true })
})
14 changes: 11 additions & 3 deletions docs/alerts.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Alerts

Alerts are how the CRM tells whoever is using it what is happening: a save the server
refused, an action that finished, work that is taking a while. They are drawn in one place,
over the bottom of the page, by the CRM's shell. The CRM's own parts raise them, and so can
your layouts, your components and your modules.
refused, an action that finished, work that is taking a while. They are drawn in one place
by the CRM's shell: the top right corner of the page, under the top bar. The CRM's own parts
raise them, and so can your layouts, your components and your modules.

That corner keeps alerts off the foot of the page: a form's buttons, a panel pinned to the
bottom and the last rows of a list stay within reach while an alert is up, one that stays
until it is dismissed included. What an alert can be over is the right end of whatever is
directly under the top bar (a list's search and its "New" button, the last tabs of a
record), until it goes or is dismissed. An alert raised while a dialog is open is drawn
behind it, and is there when the dialog closes: say what a dialog's own form got wrong in
the dialog.

## What the CRM raises on its own

Expand Down
9 changes: 7 additions & 2 deletions packages/spools/src/crm/ui/alerts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function trimmed(raised: readonly Raised[]): readonly Raised[] {
return trimmed(raised.filter((one) => one !== passing))
}

/** Holds the alerts that are up and draws them over the page. The shell has one. */
/** Holds the alerts that are up and draws them over a corner of the page. The shell has one. */
export function AlertsProvider({ children }: { children: ReactNode }) {
const [raised, setRaised] = useState<readonly Raised[]>([])
const { onAlert } = useSpoolUi()
Expand Down Expand Up @@ -187,10 +187,15 @@ export function AlertsProvider({ children }: { children: ReactNode }) {
return (
<AlertsContext value={alerts}>
{children}
{/*
In the top right corner, under the top bar: most of what people act on is at the foot of a
page (a form's buttons, a pinned panel, the last rows of a list, the bar of a selection) and
down its middle, and an alert that stays must not be over any of it.
*/}
<section
aria-label="Alerts"
data-alerts=""
className="pointer-events-none fixed bottom-4 left-1/2 z-50 flex w-96 max-w-[calc(100vw-2rem)] -translate-x-1/2 flex-col gap-2"
className="pointer-events-none fixed top-13 right-4 z-50 flex w-80 max-w-[calc(100vw-2rem)] flex-col gap-2"
>
{raised.map(({ id, alert }) => (
<AlertCard key={id} alert={alert} onDismiss={() => alerts.dismiss(id)} />
Expand Down
Loading