Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions apps/sandbox/scripts/next-export-routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"/pages/perf-scenes/",
"/pages/polymorphic-link/",
"/pages/powersearch-phone/",
"/pages/refresh-control-ab/",
"/pages/refresh-control/",
"/pages/shell-lab/",
"/pages/showcase-components/",
"/pages/showcase-header/",
Expand Down
152 changes: 152 additions & 0 deletions apps/sandbox/src/app/(raw)/pages/refresh-control-ab/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

/**
* @file page.tsx
* @input Nothing
* @output A bare page that compares two ways a pulled RefreshControl list
* returns under Reduce Motion, behind a switch. A cross-fades with a brief
* copy of the list. B uses one list: it fades out where it was, then fades
* in at rest. Rows below make the document scroll.
* @position A raw page, with no sandbox chrome, for a phone check. Preview
* only.
*/

import {useCallback, useEffect, useState} from 'react';
import * as stylex from '@stylexjs/stylex';
import {REFRESH_REVEAL_VAR, useRefreshControl} from '@astryxdesign/lab';
import {Button} from '@astryxdesign/core/Button';
import {Card} from '@astryxdesign/core/Card';
import {HStack, VStack} from '@astryxdesign/core/Layout';
import {List, ListItem} from '@astryxdesign/core/List';
import {Spinner} from '@astryxdesign/core/Spinner';
import {Heading, Text} from '@astryxdesign/core/Text';

type Form = 'A' | 'B';

const REFRESH_MS = 1500;

const LIST_ROWS = Array.from(
{length: 30},
(_, index) => `Message ${index + 1}`,
);
const PAGE_ROWS = Array.from({length: 30}, (_, index) => `Row ${index + 1}`);

const FORMS: Record<Form, string> = {
A: 'A: cross-fade with a brief copy',
B: 'B: one list, out then in',
};

const styles = stylex.create({
scroller: {height: '100%', overflowY: 'auto'},
content: {position: 'relative'},
// The indicator's band sits above the content, inside a box that clips it.
band: {
position: 'absolute',
insetInline: 0,
bottom: '100%',
height: '100lvh',
overflow: 'hidden',
pointerEvents: 'none',
},
indicator: {
position: 'absolute',
insetInline: 0,
bottom: 0,
height: 60,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
revealed: (opacity: string) => ({opacity}),
});

function useReducedMotion(): boolean {
const [isReduced, setIsReduced] = useState(false);
useEffect(() => {
const query = window.matchMedia('(prefers-reduced-motion: reduce)');
const update = () => setIsReduced(query.matches);
update();
query.addEventListener('change', update);
return () => query.removeEventListener('change', update);
}, []);
return isReduced;
}

export default function RefreshControlAbPage() {
const [form, setForm] = useState<Form>('A');
const [count, setCount] = useState(0);
const isReduced = useReducedMotion();
const onRefresh = useCallback(async () => {
await new Promise<void>(resolve => {
setTimeout(resolve, REFRESH_MS);
});
setCount(value => value + 1);
}, []);
const control = useRefreshControl({onRefresh, announce: 'Refreshing'});
return (
<VStack gap={4} padding={4}>
<VStack gap={1}>
<Heading level={1}>Refresh control under Reduce Motion</Heading>
<Text color="secondary">
Turn on Reduce Motion in Settings. Pick A or B, then pull the messages
down past the spinner and let go. Watch the list as it waits below the
spinner, and as it goes back to the top.
</Text>
<Text>{`Reduce Motion is ${isReduced ? 'on' : 'off'}.`}</Text>
</VStack>
<HStack gap={2}>
{(['A', 'B'] as const).map(option => (
<Button
key={option}
label={FORMS[option]}
variant={form === option ? 'primary' : 'secondary'}
aria-pressed={form === option}
onClick={() => setForm(option)}
/>
))}
</HStack>
<Text>{`${FORMS[form]} · Refreshed ${count} times`}</Text>
<Card height="60dvh" padding={0}>
<div
{...stylex.props(styles.scroller)}
role="region"
aria-label="Messages"
tabIndex={0}
data-refresh-reduce-motion-form={form === 'B' ? 'dip' : undefined}>
<div
ref={control.contentRef}
{...stylex.props(styles.content)}
aria-busy={control.isRefreshing || undefined}>
<List hasDividers>
{LIST_ROWS.map(row => (
<ListItem key={row} label={row} />
))}
</List>
<div {...stylex.props(styles.band)}>
<div
ref={control.indicatorRef}
aria-hidden
{...stylex.props(styles.indicator)}>
<span
{...stylex.props(
styles.revealed(`var(${REFRESH_REVEAL_VAR}, 0)`),
)}>
<Spinner size="lg" shade="subtle" />
</span>
</div>
</div>
</div>
</div>
</Card>
<VStack gap={2}>
{PAGE_ROWS.map(row => (
<Text key={row} color="secondary">
{row}
</Text>
))}
</VStack>
</VStack>
);
}
118 changes: 118 additions & 0 deletions apps/sandbox/src/app/(raw)/pages/refresh-control/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

'use client';

/**
* @file page.tsx
* @input Nothing
* @output A bare page with a RefreshControl list in a fixed-height card,
* above rows that make the document scroll. A pull past the trigger
* refreshes for 1.5 s: the list settles below the spinner, then returns to
* the top.
* @position A raw page, with no sandbox chrome, for checking a pull to refresh
* on a phone, with motion and under Reduce Motion. The page scrolls with
* the document.
*/

import {useCallback, useState} from 'react';
import * as stylex from '@stylexjs/stylex';
import {REFRESH_REVEAL_VAR, useRefreshControl} from '@astryxdesign/lab';
import {Card} from '@astryxdesign/core/Card';
import {VStack} from '@astryxdesign/core/Layout';
import {List, ListItem} from '@astryxdesign/core/List';
import {Spinner} from '@astryxdesign/core/Spinner';
import {Heading, Text} from '@astryxdesign/core/Text';

const REFRESH_MS = 1500;

const LIST_ROWS = Array.from(
{length: 30},
(_, index) => `Message ${index + 1}`,
);
const PAGE_ROWS = Array.from({length: 30}, (_, index) => `Row ${index + 1}`);

const styles = stylex.create({
scroller: {height: '100%', overflowY: 'auto'},
content: {position: 'relative'},
// The indicator's band sits above the content, inside a box that clips it.
band: {
position: 'absolute',
insetInline: 0,
bottom: '100%',
height: '100lvh',
overflow: 'hidden',
pointerEvents: 'none',
},
indicator: {
position: 'absolute',
insetInline: 0,
bottom: 0,
height: 60,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
revealed: (opacity: string) => ({opacity}),
});

export default function RefreshControlPage() {
const [count, setCount] = useState(0);
const onRefresh = useCallback(async () => {
await new Promise<void>(resolve => {
setTimeout(resolve, REFRESH_MS);
});
setCount(value => value + 1);
}, []);
const control = useRefreshControl({onRefresh, announce: 'Refreshing'});
return (
<VStack gap={4} padding={4}>
<VStack gap={1}>
<Heading level={1}>Refresh control</Heading>
<Text color="secondary">
With the messages at the top, pull them down past the spinner and let
go. They wait below the spinner while the refresh runs, then go back
to the top.
</Text>
</VStack>
<Text>{`Refreshed ${count} times`}</Text>
<Card height="60dvh" padding={0}>
<div
{...stylex.props(styles.scroller)}
role="region"
aria-label="Messages"
tabIndex={0}>
<div
ref={control.contentRef}
{...stylex.props(styles.content)}
aria-busy={control.isRefreshing || undefined}>
<List hasDividers>
{LIST_ROWS.map(row => (
<ListItem key={row} label={row} />
))}
</List>
<div {...stylex.props(styles.band)}>
<div
ref={control.indicatorRef}
aria-hidden
{...stylex.props(styles.indicator)}>
<span
{...stylex.props(
styles.revealed(`var(${REFRESH_REVEAL_VAR}, 0)`),
)}>
<Spinner size="lg" shade="subtle" />
</span>
</div>
</div>
</div>
</div>
</Card>
<VStack gap={2}>
{PAGE_ROWS.map(row => (
<Text key={row} color="secondary">
{row}
</Text>
))}
</VStack>
</VStack>
);
}
12 changes: 12 additions & 0 deletions apps/sandbox/src/app/sandboxPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ export const categories: SandboxCategory[] = [
description:
'An alert over a toast and a dialog closed after a save, on a page that scrolls with the document, to check on a phone',
},
{
name: 'Refresh Control',
href: '/pages/refresh-control/',
description:
'A RefreshControl list in a card on a page that scrolls with the document, to check a pull to refresh on a phone, also under Reduce Motion',
},
{
name: 'Refresh Control A/B',
href: '/pages/refresh-control-ab/',
description:
'Two ways a pulled RefreshControl list returns under Reduce Motion, behind a switch, on a page that scrolls with the document',
},
{
name: 'BottomSheet Pull',
href: '/pages/bottom-sheet-pull/',
Expand Down
5 changes: 4 additions & 1 deletion docs/architecture/component-style-authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ not evidence of conformance or violation.
handoff between sheets, such as the cross-fade
`design:reduced-motion` DR2 requires; AppShell's old content wrap during a
side nav collapse or expand (`component:AppShell` FR17); the SideNav rail's
old state during a collapse or expand (`component:SideNav` FR6); or one moving preview while a
old state during a collapse or expand (`component:SideNav` FR6); the old
place of content that a release moves under Reduce Motion, fading out as the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"content that a release moves" leaves out the refresh's end, which also lays a copy (refreshTouchController.ts:644, crossFadeContent(finish, false)) and is not started by a release. Word the item for both moves, or keep the animator's form for the end.

content fades in at its new place, such as RefreshControl's list
(`design:reduced-motion` DR2); or one moving preview while a
reorderable item is dragged (`design:ordered-collection-reordering`); it is
made by one Core mechanism for its kind, rather than by each component, and is removed within 1
second of the transition ending. Until that shared mechanism exists, a
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/internal/touch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ describe('internal/touch', () => {
);
});

it("re-exports by name the cross-fade Lab's RefreshControl plays", async () => {
const entry: Record<string, unknown> = await import('./touch');
const crossFade = await import('../motion/reducedMotionCrossFade');
// Identity alone passes when both sides are undefined.
expect(typeof entry.playReducedMotionCrossFade).toBe('function');
expect(entry.playReducedMotionCrossFade).toBe(
crossFade.playReducedMotionCrossFade,
);
});

it('attaches no window or document listeners when it loads', async () => {
vi.resetModules();
const onWindow = vi.spyOn(window, 'addEventListener');
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/internal/touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,8 @@ export type {ActionGroupTrackingOptions} from '../hooks/useActionGroupTracking';

// Lab's TabBar finds a panel's scroll area with Core's scroll test.
export {measureLogicalScrollAxis} from '../hooks/scrollGeometry';

// Lab's RefreshControl cross-fades its list through a copy under Reduce
// Motion, so the list shows on every frame.
export {playReducedMotionCrossFade} from '../motion/reducedMotionCrossFade';
export type {ReducedMotionCrossFade} from '../motion/reducedMotionCrossFade';
3 changes: 2 additions & 1 deletion packages/core/src/motion/animatedLayout.boundary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('architecture:react-component-runtime INV12 source boundary', () => {
}
});

it("keeps the copy mechanism to the swap and the layer's exit copy", () => {
it("keeps the copy mechanism to the swap, the layer's exit copy and the Reduce Motion cross-fade", () => {
const users = sourceFiles(coreSrc)
.filter(path =>
/from '[./]+(motion\/)?transientCopy'/.test(readFileSync(path, 'utf8')),
Expand All @@ -146,6 +146,7 @@ describe('architecture:react-component-runtime INV12 source boundary', () => {
expect(users).toEqual([
'Layer/layerExitCopy.ts',
'motion/animatedLayout.ts',
'motion/reducedMotionCrossFade.ts',
]);
});

Expand Down
Loading