@estiva-app/ui 0.93.0 → 0.95.0
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.
- package/dist/CollapsibleSection.d.ts +19 -2
- package/dist/CollapsibleSection.d.ts.map +1 -1
- package/dist/LinkRow.d.ts.map +1 -1
- package/dist/NavItem.d.ts +7 -1
- package/dist/NavItem.d.ts.map +1 -1
- package/dist/NavTree.d.ts +25 -3
- package/dist/NavTree.d.ts.map +1 -1
- package/dist/PartCard.d.ts +4 -7
- package/dist/PartCard.d.ts.map +1 -1
- package/dist/ShowMore.d.ts +1 -7
- package/dist/ShowMore.d.ts.map +1 -1
- package/dist/index.js +280 -224
- package/dist/index.js.map +4 -4
- package/package.json +1 -1
- package/registry.json +26 -14
- package/src/CollapsibleSection.tsx +30 -3
- package/src/LinkCard.test.tsx +5 -2
- package/src/LinkRow.mdx +1 -1
- package/src/LinkRow.tsx +10 -5
- package/src/NavItem.tsx +9 -0
- package/src/NavTree.mdx +31 -2
- package/src/NavTree.stories.tsx +140 -2
- package/src/NavTree.test.tsx +75 -0
- package/src/NavTree.tsx +102 -10
- package/src/PartCard.mdx +1 -1
- package/src/PartCard.stories.tsx +2 -9
- package/src/PartCard.test.tsx +13 -1
- package/src/PartCard.tsx +6 -9
- package/src/ShowMore.mdx +2 -5
- package/src/ShowMore.stories.tsx +0 -14
- package/src/ShowMore.test.tsx +0 -16
- package/src/ShowMore.tsx +2 -16
package/src/NavTree.tsx
CHANGED
|
@@ -84,8 +84,19 @@ export interface NavTreeProps {
|
|
|
84
84
|
* is absent.
|
|
85
85
|
*/
|
|
86
86
|
children?: ReactNode
|
|
87
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* The row that is current: `active`. Each time it names a new row, its
|
|
89
|
+
* group and every row above it open, closing nothing — a group closed by
|
|
90
|
+
* hand too — and the column scrolls to it once they have slid open.
|
|
91
|
+
*/
|
|
88
92
|
selected?: string
|
|
93
|
+
/**
|
|
94
|
+
* The row the current one sits under, while the tree does not draw the
|
|
95
|
+
* current one: its rows are not read yet, or it is not a row here. The tree
|
|
96
|
+
* opens onto that row the same way, opens the row itself so its rows are
|
|
97
|
+
* read, and marks nothing for it.
|
|
98
|
+
*/
|
|
99
|
+
selectedParent?: string
|
|
89
100
|
/** A row was clicked. A router app prevents the default and navigates. */
|
|
90
101
|
onSelect?: (node: NavTreeNode, event: MouseEvent<HTMLAnchorElement>) => void
|
|
91
102
|
/** What to say when `groups` is empty. The words are the caller's; absent, nothing is said. */
|
|
@@ -97,10 +108,58 @@ export interface NavTreeProps {
|
|
|
97
108
|
/** How long the pointer rests on a group before `onIntent` (Peek's FolderTree, 150ms). */
|
|
98
109
|
export const NAV_TREE_INTENT_MS = 150
|
|
99
110
|
|
|
111
|
+
/** How long the column waits to scroll to the current row: the rows above it slide open in 150ms. */
|
|
112
|
+
export const NAV_TREE_REVEAL_MS = 200
|
|
113
|
+
|
|
100
114
|
function holds(nodes: NavTreeNode[] | null, id: string | undefined): boolean {
|
|
101
115
|
return !!nodes && id !== undefined && nodes.some((node) => node.id === id || holds(node.children ?? null, id))
|
|
102
116
|
}
|
|
103
117
|
|
|
118
|
+
/** The rows above the row `id`, outermost first; `null` when no row here is `id`. */
|
|
119
|
+
function rowsAbove(nodes: NavTreeNode[] | null | undefined, id: string): string[] | null {
|
|
120
|
+
for (const node of nodes ?? []) {
|
|
121
|
+
if (node.id === id) return []
|
|
122
|
+
const inner = rowsAbove(node.children, id)
|
|
123
|
+
if (inner) return [node.id, ...inner]
|
|
124
|
+
}
|
|
125
|
+
return null
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** What opens so the current row shows: its group and the rows above it — or above `selectedParent`, and that row, until the current one is drawn. */
|
|
129
|
+
interface Opening {
|
|
130
|
+
/** The current item, for `openFor`: a new one opens them again. */
|
|
131
|
+
item: string
|
|
132
|
+
group: string
|
|
133
|
+
rows: ReadonlySet<string>
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function openingFor(groups: NavTreeGroup[], selected: string | undefined, selectedParent: string | undefined): Opening | null {
|
|
137
|
+
const item = selected ?? selectedParent
|
|
138
|
+
if (item === undefined) return null
|
|
139
|
+
for (const [target, andItself] of [[selected, false], [selectedParent, true]] as const) {
|
|
140
|
+
if (target === undefined) continue
|
|
141
|
+
for (const group of groups) {
|
|
142
|
+
const above = rowsAbove(group.nodes, target)
|
|
143
|
+
if (above) return { item, group: group.id, rows: new Set(andItself ? [...above, target] : above) }
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return null
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Scroll the column the row is in, and only that box — never the page, nor a
|
|
151
|
+
* drawer that is shut — so the row is in sight. A row in sight stays put.
|
|
152
|
+
*/
|
|
153
|
+
function scrollToRow(row: HTMLElement) {
|
|
154
|
+
let box = row.parentElement
|
|
155
|
+
while (box && !(/(auto|scroll)/.test(getComputedStyle(box).overflowY) && box.scrollHeight > box.clientHeight)) box = box.parentElement
|
|
156
|
+
if (!box || box === document.documentElement || box === document.body) return
|
|
157
|
+
const at = row.getBoundingClientRect()
|
|
158
|
+
const view = box.getBoundingClientRect()
|
|
159
|
+
if (at.top < view.top) box.scrollTop -= view.top - at.top
|
|
160
|
+
else if (at.bottom > view.bottom) box.scrollTop += at.bottom - view.bottom
|
|
161
|
+
}
|
|
162
|
+
|
|
104
163
|
/** A line in the rows' place: level with the rows, which pad themselves (Katerina, 14 September). */
|
|
105
164
|
function Line({ message }: { message: string }) {
|
|
106
165
|
return (
|
|
@@ -110,12 +169,14 @@ function Line({ message }: { message: string }) {
|
|
|
110
169
|
)
|
|
111
170
|
}
|
|
112
171
|
|
|
113
|
-
|
|
172
|
+
type Drawn = Pick<NavTreeProps, 'selected' | 'onSelect'> & { opening: Opening | null }
|
|
173
|
+
|
|
174
|
+
function Row({ node, selected, onSelect, opening }: { node: NavTreeNode } & Drawn) {
|
|
114
175
|
const under =
|
|
115
176
|
node.childrenMessage !== undefined ? (
|
|
116
177
|
<Line message={node.childrenMessage} />
|
|
117
178
|
) : node.children?.length ? (
|
|
118
|
-
node.children.map((child) => <Row key={child.id} node={child} selected={selected} onSelect={onSelect} />)
|
|
179
|
+
node.children.map((child) => <Row key={child.id} node={child} selected={selected} onSelect={onSelect} opening={opening} />)
|
|
119
180
|
) : node.children === null || (node.hasChildren && node.children === undefined) ? (
|
|
120
181
|
<SkeletonList rows={2} />
|
|
121
182
|
) : undefined
|
|
@@ -134,6 +195,7 @@ function Row({ node, selected, onSelect }: { node: NavTreeNode } & Pick<NavTreeP
|
|
|
134
195
|
defaultOpen={node.defaultOpen ?? true}
|
|
135
196
|
open={node.open}
|
|
136
197
|
onOpenChange={node.onOpenChange}
|
|
198
|
+
openFor={opening?.rows.has(node.id) ? opening.item : undefined}
|
|
137
199
|
onClick={onSelect ? (event) => onSelect(node, event) : undefined}
|
|
138
200
|
>
|
|
139
201
|
{under}
|
|
@@ -141,7 +203,7 @@ function Row({ node, selected, onSelect }: { node: NavTreeNode } & Pick<NavTreeP
|
|
|
141
203
|
)
|
|
142
204
|
}
|
|
143
205
|
|
|
144
|
-
function Group({ group, selected, onSelect, storageKey }: { group: NavTreeGroup } & Pick<NavTreeProps, '
|
|
206
|
+
function Group({ group, selected, onSelect, opening, storageKey }: { group: NavTreeGroup } & Drawn & Pick<NavTreeProps, 'storageKey'>) {
|
|
145
207
|
return (
|
|
146
208
|
<NavTreeSection
|
|
147
209
|
id={group.id}
|
|
@@ -156,9 +218,10 @@ function Group({ group, selected, onSelect, storageKey }: { group: NavTreeGroup
|
|
|
156
218
|
open={group.open}
|
|
157
219
|
onOpenChange={group.onOpenChange}
|
|
158
220
|
storageKey={storageKey ? `${storageKey}.${group.id}` : undefined}
|
|
221
|
+
openFor={opening?.group === group.id ? opening.item : undefined}
|
|
159
222
|
onIntent={group.onIntent}
|
|
160
223
|
>
|
|
161
|
-
{group.nodes?.map((node) => <Row key={node.id} node={node} selected={selected} onSelect={onSelect} />)}
|
|
224
|
+
{group.nodes?.map((node) => <Row key={node.id} node={node} selected={selected} onSelect={onSelect} opening={opening} />)}
|
|
162
225
|
</NavTreeSection>
|
|
163
226
|
)
|
|
164
227
|
}
|
|
@@ -194,6 +257,8 @@ export interface NavTreeSectionProps {
|
|
|
194
257
|
pending?: boolean
|
|
195
258
|
/** Remember it open or closed in this browser, under this key. */
|
|
196
259
|
storageKey?: string
|
|
260
|
+
/** The current item, when it sits in this group: the group opens each time this names a new one — `CollapsibleSection`'s. */
|
|
261
|
+
openFor?: string
|
|
197
262
|
/** The rows: `NavItem`s. */
|
|
198
263
|
children?: ReactNode
|
|
199
264
|
}
|
|
@@ -205,7 +270,7 @@ export interface NavTreeSectionProps {
|
|
|
205
270
|
* `groups`; an app whose groups each read their own data draws them itself,
|
|
206
271
|
* as `NavTree`'s children.
|
|
207
272
|
*/
|
|
208
|
-
export function NavTreeSection({ id, title, message, loading = false, pending, trailing, actions, menu, icon, defaultOpen = true, open, onOpenChange, storageKey, onIntent, children }: NavTreeSectionProps) {
|
|
273
|
+
export function NavTreeSection({ id, title, message, loading = false, pending, trailing, actions, menu, icon, defaultOpen = true, open, onOpenChange, storageKey, openFor, onIntent, children }: NavTreeSectionProps) {
|
|
209
274
|
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
|
|
210
275
|
useEffect(() => () => clearTimeout(timer.current), [])
|
|
211
276
|
const intent = onIntent
|
|
@@ -233,6 +298,7 @@ export function NavTreeSection({ id, title, message, loading = false, pending, t
|
|
|
233
298
|
onOpenChange={onOpenChange}
|
|
234
299
|
pending={pending}
|
|
235
300
|
storageKey={storageKey}
|
|
301
|
+
openFor={openFor}
|
|
236
302
|
trailing={trailing}
|
|
237
303
|
actions={actions}
|
|
238
304
|
menu={menu}
|
|
@@ -255,10 +321,36 @@ export function NavTreeSection({ id, title, message, loading = false, pending, t
|
|
|
255
321
|
* arrow that folds what is under it. Groups and rows can be read when they
|
|
256
322
|
* open. Only the group that holds the current row starts open, unless the
|
|
257
323
|
* caller owns it.
|
|
324
|
+
*
|
|
325
|
+
* Opening a row shows where it lives (Katerina, 30 October: a file opened
|
|
326
|
+
* from another app's card, in a new tab, sat marked inside a shut Folder):
|
|
327
|
+
* each new current row opens its group and the rows above it, even those
|
|
328
|
+
* closed by hand, once — closed again, they stay closed until another opens —
|
|
329
|
+
* and the column scrolls to it. Groups the app draws itself take `openFor`
|
|
330
|
+
* for that; the scroll is the tree's either way.
|
|
258
331
|
*/
|
|
259
|
-
export function NavTree({ title, titleActions, groups, children, selected, onSelect, emptyMessage = '', storageKey }: NavTreeProps) {
|
|
332
|
+
export function NavTree({ title, titleActions, groups, children, selected, selectedParent, onSelect, emptyMessage = '', storageKey }: NavTreeProps) {
|
|
333
|
+
const opening = groups ? openingFor(groups, selected, selectedParent) : null
|
|
334
|
+
// Once per current row, when it is drawn and shown: a later draw, or a read landing, does not pull the column back to it.
|
|
335
|
+
const box = useRef<HTMLDivElement>(null)
|
|
336
|
+
const scrolled = useRef<string | undefined>(undefined)
|
|
337
|
+
const waiting = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
|
|
338
|
+
const latest = useRef(selected)
|
|
339
|
+
latest.current = selected
|
|
340
|
+
useEffect(() => {
|
|
341
|
+
if (selected === undefined || scrolled.current === selected || waiting.current !== undefined) return
|
|
342
|
+
waiting.current = setTimeout(() => {
|
|
343
|
+
waiting.current = undefined
|
|
344
|
+
const row = box.current?.querySelector<HTMLElement>('[aria-current="page"]')
|
|
345
|
+
if (!row || row.getBoundingClientRect().height === 0) return
|
|
346
|
+
scrolled.current = latest.current
|
|
347
|
+
scrollToRow(row)
|
|
348
|
+
}, NAV_TREE_REVEAL_MS)
|
|
349
|
+
})
|
|
350
|
+
useEffect(() => () => clearTimeout(waiting.current), [])
|
|
260
351
|
return (
|
|
261
|
-
|
|
352
|
+
// `contents`: a box to find the current row in, that lays nothing out — the groups stay the column's own.
|
|
353
|
+
<div ref={box} className="contents">
|
|
262
354
|
{title && <SectionHeader title={title} look="quiet" showActions="always" actions={titleActions} className="mt-4 shrink-0" />}
|
|
263
355
|
{groups === undefined ? (
|
|
264
356
|
children
|
|
@@ -268,8 +360,8 @@ export function NavTree({ title, titleActions, groups, children, selected, onSel
|
|
|
268
360
|
// No words, no line: an app that says nothing when it has no groups.
|
|
269
361
|
emptyMessage ? <Line message={emptyMessage} /> : null
|
|
270
362
|
) : (
|
|
271
|
-
groups.map((group) => <Group key={group.id} group={group} selected={selected} onSelect={onSelect} storageKey={storageKey} />)
|
|
363
|
+
groups.map((group) => <Group key={group.id} group={group} selected={selected} onSelect={onSelect} opening={opening} storageKey={storageKey} />)
|
|
272
364
|
)}
|
|
273
|
-
|
|
365
|
+
</div>
|
|
274
366
|
)
|
|
275
367
|
}
|
package/src/PartCard.mdx
CHANGED
|
@@ -12,7 +12,7 @@ A part of something longer, shared live: its blocks as they are now, under the t
|
|
|
12
12
|
## When
|
|
13
13
|
|
|
14
14
|
- **ready** — what it is part of, as a card starts: its kind in a file's 36px tile, its title as the link, and "A part of ABC-2" under it; then the blocks the app draws as `children`, behind a quote bar that starts where the tile does. The apps draw the blocks with **RichText** `size="small"`.
|
|
15
|
-
- **
|
|
15
|
+
- **long** — with `maxHeight`, a long part is cut behind Show more, under the quote bar. Its title is the way to it, in the app it lives in: there is no "Open in …" button (Katerina, 31 Oct).
|
|
16
16
|
- **loading** — at a card's height.
|
|
17
17
|
- **unavailable** — one row: "A part of this issue not available", in the app's word for it, else its kind's. Not the reader's to see.
|
|
18
18
|
- **deleted** — what it is part of was deleted, as a trusted deletion naming it says (SPEC §13.6.1): the same row, "A part of this issue deleted".
|
package/src/PartCard.stories.tsx
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react-vite'
|
|
2
|
-
import { IconExternalLink } from '@tabler/icons-react'
|
|
3
|
-
import { Button } from './Button'
|
|
4
2
|
import { PartCard } from './PartCard'
|
|
5
3
|
import { RichText, type RichTextBlock } from './RichText'
|
|
6
4
|
|
|
@@ -38,19 +36,14 @@ const LONG_BLOCKS: RichTextBlock[] = [
|
|
|
38
36
|
...Array.from({ length: 6 }, (_, i): RichTextBlock => ({ type: 'paragraph', inline: [{ text: `Paragraph ${i + 1} of the part, long enough to fill a line of the card and go on to a second one.`, marks: [] }] })),
|
|
39
37
|
]
|
|
40
38
|
|
|
41
|
-
/** As an app quotes a long part: cut behind Show more,
|
|
42
|
-
export const
|
|
39
|
+
/** As an app quotes a long part: cut behind Show more, under the quote bar. Its title is the way to it, in its own app. */
|
|
40
|
+
export const Long: Story = {
|
|
43
41
|
args: {
|
|
44
42
|
state: 'ready',
|
|
45
43
|
kind: 'page',
|
|
46
44
|
heading: 'Document one',
|
|
47
45
|
href: '#part',
|
|
48
46
|
maxHeight: 160,
|
|
49
|
-
action: (
|
|
50
|
-
<Button size="small" leadingIcon={<IconExternalLink stroke={1.5} className="size-3.5" />} onClick={() => {}}>
|
|
51
|
-
Open in App one
|
|
52
|
-
</Button>
|
|
53
|
-
),
|
|
54
47
|
children: <RichText blocks={LONG_BLOCKS} size="small" />,
|
|
55
48
|
},
|
|
56
49
|
}
|
package/src/PartCard.test.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
/** What the PartCard page claims, pinned: the header opens what it is part of, the blocks draw, and the states say so. */
|
|
3
3
|
import { cleanup, render, screen } from '@testing-library/react'
|
|
4
|
-
import { afterEach, describe, expect, it } from 'vitest'
|
|
4
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
5
5
|
import { PartCard } from './PartCard'
|
|
6
6
|
|
|
7
7
|
afterEach(cleanup)
|
|
@@ -19,6 +19,18 @@ describe('PartCard', () => {
|
|
|
19
19
|
expect(screen.getByText('The blocks')).toBeTruthy()
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
+
it('a long part is cut behind Show more, its title the only way to it (Katerina, 31 Oct: no "Open in" beside it)', () => {
|
|
23
|
+
const tall = vi.spyOn(HTMLElement.prototype, 'scrollHeight', 'get').mockReturnValue(300)
|
|
24
|
+
render(
|
|
25
|
+
<PartCard kind="page" heading="Document one" href="#part" maxHeight={160}>
|
|
26
|
+
<p>The blocks</p>
|
|
27
|
+
</PartCard>,
|
|
28
|
+
)
|
|
29
|
+
expect(screen.getAllByRole('button').map((b) => b.textContent)).toEqual(['Show more'])
|
|
30
|
+
expect(screen.getAllByRole('link').map((a) => a.textContent)).toEqual(['Document one'])
|
|
31
|
+
tall.mockRestore()
|
|
32
|
+
})
|
|
33
|
+
|
|
22
34
|
it('says a part is not available in words', () => {
|
|
23
35
|
render(<PartCard state="unavailable" kind="issue" noun="Issue" />)
|
|
24
36
|
expect(screen.getByText('A part of this issue not available')).toBeTruthy()
|
package/src/PartCard.tsx
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
* Look C (Katerina, 29 Oct): what it is part of as a card starts, its kind in a
|
|
9
9
|
* file's 36px tile, its title and "A part of ABC-2" under it; then the blocks
|
|
10
10
|
* behind a quote bar that starts where the tile does.
|
|
11
|
+
*
|
|
12
|
+
* The title is the way to it, in the app it lives in: no "Open in …" button
|
|
13
|
+
* beside the blocks (Katerina, 31 Oct: it said again what the title does).
|
|
11
14
|
*/
|
|
12
15
|
import { useContext, type ReactNode } from 'react'
|
|
13
16
|
import { Card } from './Card'
|
|
@@ -45,17 +48,11 @@ export interface PartCardProps {
|
|
|
45
48
|
openIn?: { app: string; href: string }
|
|
46
49
|
/** The part's blocks, drawn by the app. */
|
|
47
50
|
children?: ReactNode
|
|
48
|
-
/**
|
|
49
|
-
* The way out to where the part lives: a small `Button` with a leading icon ("Open in Leaf"). Drawn under the
|
|
50
|
-
* blocks at the right, level with Show more, outside the quote bar (Katerina, 30 Oct: "the vertical line should not
|
|
51
|
-
* be covering the button"). Peek and Ship put it in ShowMore's row inside the blocks before.
|
|
52
|
-
*/
|
|
53
|
-
action?: ReactNode
|
|
54
51
|
/** Cut the blocks at this height, in px, behind ShowMore's fade and its Show more. Absent, they are drawn whole. */
|
|
55
52
|
maxHeight?: number
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
export function PartCard({ state = 'ready', kind, heading, href, partKey, noun, openIn, children,
|
|
55
|
+
export function PartCard({ state = 'ready', kind, heading, href, partKey, noun, openIn, children, maxHeight }: PartCardProps) {
|
|
59
56
|
const word = noun ?? kindNounOf({ kind }) ?? 'File'
|
|
60
57
|
const boxed = useContext(inTheBox)
|
|
61
58
|
if (state === 'loading') return <LinkCardLoading kind={kind} shape={{ summary: true, rows: 0, footer: 'none' }} />
|
|
@@ -85,8 +82,8 @@ export function PartCard({ state = 'ready', kind, heading, href, partKey, noun,
|
|
|
85
82
|
</span>
|
|
86
83
|
</div>
|
|
87
84
|
{/* The quote bar starts where the tile above it starts (Katerina, 29 Oct), and runs beside the blocks only. */}
|
|
88
|
-
{
|
|
89
|
-
<ShowMore maxHeight={maxHeight
|
|
85
|
+
{maxHeight !== undefined ? (
|
|
86
|
+
<ShowMore maxHeight={maxHeight} contentClassName={QUOTE_BAR_CLASSES}>
|
|
90
87
|
{children}
|
|
91
88
|
</ShowMore>
|
|
92
89
|
) : (
|
package/src/ShowMore.mdx
CHANGED
|
@@ -44,11 +44,8 @@ import { ShowMore } from '@estiva-app/ui'
|
|
|
44
44
|
- `maxHeight` is in px, 96 by default.
|
|
45
45
|
- The toggle is a small outlined `Button` with a chevron: down on "Show more",
|
|
46
46
|
up on "Show less". Its edge starts where the content does.
|
|
47
|
-
-
|
|
48
|
-
|
|
49
|
-
row is drawn for it even when the content fits and there is no toggle.
|
|
50
|
-
|
|
51
|
-
<Canvas of={ShowMoreStories.WithAnAction} />
|
|
47
|
+
- Nothing else goes on the toggle's row. The way to where the content lives is
|
|
48
|
+
its title, above it.
|
|
52
49
|
|
|
53
50
|
<Canvas of={ShowMoreStories.InACard} />
|
|
54
51
|
|
package/src/ShowMore.stories.tsx
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from '@storybook/react-vite'
|
|
2
|
-
import { IconExternalLink } from '@tabler/icons-react'
|
|
3
|
-
import { Button } from './Button'
|
|
4
2
|
import { Card } from './Card'
|
|
5
3
|
import { ShowMore } from './ShowMore'
|
|
6
4
|
|
|
@@ -39,15 +37,3 @@ export const InACard: Story = {
|
|
|
39
37
|
</Card>
|
|
40
38
|
),
|
|
41
39
|
}
|
|
42
|
-
|
|
43
|
-
/** `action`: one more small button, level with Show more at the right of its row. */
|
|
44
|
-
export const WithAnAction: Story = {
|
|
45
|
-
args: { surface: 'inset', action: <Button size="small" leadingIcon={<IconExternalLink stroke={1.5} className="size-3.5" />}>Open in Estiva Ship</Button> },
|
|
46
|
-
render: InACard.render,
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Short content with an action: no toggle, and the action keeps its place at the right. */
|
|
50
|
-
export const ShortWithAnAction: Story = {
|
|
51
|
-
args: { ...WithAnAction.args, children: <p className="text-caption text-text-secondary">One line of description.</p> },
|
|
52
|
-
render: InACard.render,
|
|
53
|
-
}
|
package/src/ShowMore.test.tsx
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
7
7
|
import { cleanup, render, screen } from '@testing-library/react'
|
|
8
8
|
import userEvent from '@testing-library/user-event'
|
|
9
|
-
import { Button } from './Button'
|
|
10
9
|
import { ShowMore } from './ShowMore'
|
|
11
10
|
|
|
12
11
|
afterEach(() => {
|
|
@@ -42,19 +41,4 @@ describe('ShowMore', () => {
|
|
|
42
41
|
expect(screen.queryByRole('button')).toBeNull()
|
|
43
42
|
expect(screen.getByText('Short').parentElement!.style.maxHeight).toBe('')
|
|
44
43
|
})
|
|
45
|
-
|
|
46
|
-
it('puts an action level with the toggle, at the right of its row', () => {
|
|
47
|
-
contentHeight(300)
|
|
48
|
-
render(<ShowMore maxHeight={96} action={<Button>Open</Button>}><p>Long</p></ShowMore>)
|
|
49
|
-
const row = screen.getByRole('button', { name: 'Show more' }).parentElement!
|
|
50
|
-
expect(row.contains(screen.getByRole('button', { name: 'Open' }))).toBe(true)
|
|
51
|
-
expect(row.className).toMatch(/justify-between/)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('keeps the action when the content fits, with no toggle', () => {
|
|
55
|
-
contentHeight(40)
|
|
56
|
-
render(<ShowMore maxHeight={96} action={<Button>Open</Button>}><p>Short</p></ShowMore>)
|
|
57
|
-
expect(screen.queryByRole('button', { name: /Show/ })).toBeNull()
|
|
58
|
-
expect(screen.getByRole('button', { name: 'Open' })).toBeTruthy()
|
|
59
|
-
})
|
|
60
44
|
})
|
package/src/ShowMore.tsx
CHANGED
|
@@ -18,10 +18,6 @@ import { cn } from './cn'
|
|
|
18
18
|
*
|
|
19
19
|
* Focus arriving inside the cut content opens it: a field or a link there,
|
|
20
20
|
* reached by Tab or a click, is never worked on out of sight.
|
|
21
|
-
*
|
|
22
|
-
* `action` is one more small button on the toggle's row, at its right
|
|
23
|
-
* (Katerina, 8 October: Peek's block card puts "Open in Estiva Ship" level with
|
|
24
|
-
* Show more). The row is drawn for it whether or not the content is cut.
|
|
25
21
|
*/
|
|
26
22
|
export interface ShowMoreProps {
|
|
27
23
|
children: ReactNode
|
|
@@ -32,8 +28,6 @@ export interface ShowMoreProps {
|
|
|
32
28
|
* inset card and a selected message included. Accepted, and ignored.
|
|
33
29
|
*/
|
|
34
30
|
surface?: 'surface' | 'inset' | 'elevated'
|
|
35
|
-
/** A small muted `Button` with a leading icon, at the right of the toggle's row: the way out to where the content lives. Its words end where the content does. */
|
|
36
|
-
action?: ReactNode
|
|
37
31
|
/** On the content alone, not the toggle's row under it: PartCard's quote bar runs beside the blocks only. */
|
|
38
32
|
contentClassName?: string
|
|
39
33
|
className?: string
|
|
@@ -42,7 +36,7 @@ export interface ShowMoreProps {
|
|
|
42
36
|
/** The last 40px of cut content fade out: a mask on the content, not a fill over it, so it ends in whatever is behind. */
|
|
43
37
|
const FADE = 'linear-gradient(to top, transparent, black 40px)'
|
|
44
38
|
|
|
45
|
-
export function ShowMore({ children, maxHeight = 96,
|
|
39
|
+
export function ShowMore({ children, maxHeight = 96, contentClassName, className }: ShowMoreProps) {
|
|
46
40
|
const box = useRef<HTMLDivElement>(null)
|
|
47
41
|
const [open, setOpen] = useState(false)
|
|
48
42
|
const [overflows, setOverflows] = useState(false)
|
|
@@ -64,15 +58,7 @@ export function ShowMore({ children, maxHeight = 96, action, contentClassName, c
|
|
|
64
58
|
<div ref={box} className={cn('relative w-full', cut && 'overflow-hidden', contentClassName)} style={cut ? { maxHeight, maskImage: FADE, WebkitMaskImage: FADE } : undefined} onFocus={cut ? () => setOpen(true) : undefined}>
|
|
65
59
|
{children}
|
|
66
60
|
</div>
|
|
67
|
-
{
|
|
68
|
-
<div className={cn('mt-1 flex w-full items-center gap-2', overflows ? 'justify-between' : 'justify-end')}>
|
|
69
|
-
{overflows && <Toggle open={open} onToggle={() => setOpen((o) => !o)} />}
|
|
70
|
-
{/* `-mr-2`: a small button's 8px right inset beside a leading icon, so its words end where the content's do. */}
|
|
71
|
-
<div className="-mr-2 flex shrink-0">{action}</div>
|
|
72
|
-
</div>
|
|
73
|
-
) : (
|
|
74
|
-
overflows && <Toggle open={open} onToggle={() => setOpen((o) => !o)} className="mt-1" />
|
|
75
|
-
)}
|
|
61
|
+
{overflows && <Toggle open={open} onToggle={() => setOpen((o) => !o)} className="mt-1" />}
|
|
76
62
|
</div>
|
|
77
63
|
)
|
|
78
64
|
}
|