Skip to content

Commit fe670fe

Browse files
committed
feat(lab): PageControl — UIPageControl's indicators, one page per tap
1 parent 290684f commit fe670fe

15 files changed

Lines changed: 939 additions & 0 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[experimental] The UIKit constants table read through `@astryxdesign/core/motion/experimental` gains `uikit.pageControl`: UIPageControl's measured indicator geometry, height and width cap, default indicator alphas and tap rule, each with its source. Nothing else changes.
6+
7+
@vjeux
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
import {useState} from 'react';
4+
import type {Meta, StoryObj} from '@storybook/react';
5+
import * as stylex from '@stylexjs/stylex';
6+
import {PageControl} from '@astryxdesign/lab';
7+
import {
8+
colorVars,
9+
radiusVars,
10+
spacingVars,
11+
typographyVars,
12+
} from '@astryxdesign/core/theme/tokens.stylex';
13+
14+
const styles = stylex.create({
15+
stack: {
16+
display: 'flex',
17+
flexDirection: 'column',
18+
alignItems: 'center',
19+
gap: spacingVars['--spacing-3'],
20+
padding: spacingVars['--spacing-6'],
21+
fontFamily: typographyVars['--font-family-body'],
22+
},
23+
page: {
24+
width: 280,
25+
height: 160,
26+
display: 'grid',
27+
placeItems: 'center',
28+
borderRadius: radiusVars['--radius-element'],
29+
backgroundColor: colorVars['--color-background-muted'],
30+
color: colorVars['--color-text-primary'],
31+
fontSize: 20,
32+
fontWeight: 600,
33+
},
34+
});
35+
36+
const meta: Meta<typeof PageControl> = {
37+
title: 'Lab/PageControl',
38+
component: PageControl,
39+
parameters: {layout: 'centered'},
40+
};
41+
42+
export default meta;
43+
type Story = StoryObj<typeof PageControl>;
44+
45+
function Paged({count, start = 0}: {count: number; start?: number}) {
46+
const [page, setPage] = useState(start);
47+
return (
48+
<div {...stylex.props(styles.stack)}>
49+
<div {...stylex.props(styles.page)} data-testid="page">
50+
Page {page + 1}
51+
</div>
52+
<PageControl count={count} value={page} onChange={setPage} />
53+
</div>
54+
);
55+
}
56+
57+
export const Default: Story = {
58+
render: () => <Paged count={5} start={2} />,
59+
};
60+
61+
export const ManyPages: Story = {
62+
name: 'Many Pages (50)',
63+
render: () => <Paged count={50} start={25} />,
64+
};
65+
66+
export const SinglePageHidden: Story = {
67+
name: 'Single Page (hidesForSinglePage)',
68+
render: () => (
69+
<PageControl count={1} value={0} onChange={() => {}} hidesForSinglePage />
70+
),
71+
};

‎packages/core/src/motion/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ from the package entry point; components import it by path.
1717
| `uikit/timing.ts` | Non-spring clocks and curves: press highlight, Core Animation's named curves, keyboard, Reduced Motion cross-fade. |
1818
| `uikit/navigation.ts` | UINavigationController: push velocity, parallax, dimming, transition clock, interactive pop finish rule. |
1919
| `uikit/gestures.ts` | Gesture recognizer thresholds, the velocity filter and the haptic stand-ins, as typed entries the gesture system reads. |
20+
| `uikit/pageControl.ts` | UIPageControl: indicator geometry, the control's height and width cap, default indicator alphas, the tap rule. |
2021
| `MEASUREMENTS.md` | The UIKit reference captures each unconfirmed value still needs. |
2122
| `spring.ts` | Closed-form damped spring, every Apple parameterization, settling duration, a spring chasing a moving target. |
2223
| `decay.ts` | UIScrollView's momentum coast. |

‎packages/core/src/motion/uikit/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export {
2929
type SourcedGestureConstant,
3030
} from './gestures';
3131
export {navigation} from './navigation';
32+
export {pageControl} from './pageControl';
3233
export {scroll} from './scroll';
3334
export {springs} from './springs';
3435
export {timing} from './timing';
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file pageControl.ts
5+
* @input Nothing; data only
6+
* @output Exports `pageControl`: UIPageControl's measured geometry, its
7+
* default indicator alphas and its tap rule
8+
* @position The page-control domain of the UIKit constants table
9+
* (motion/uikit); read by the lab PageControl
10+
*
11+
* Every value was read from a stock UIPageControl (default style, default
12+
* tint) on the iOS 26.2 simulator, iPhone 17 Pro: its frames,
13+
* `intrinsicContentSize`, `size(forNumberOfPages:)`, the indicator images'
14+
* rendered pixels, and `valueChanged` under HID touches. Geometry is in
15+
* points.
16+
*
17+
* Every leaf carries one or more `@source`; uikit.test.ts enforces it.
18+
*
19+
* SYNC: When modified, update:
20+
* - /packages/lab/src/PageControl/pageControlModel.ts
21+
* - /packages/core/src/motion/README.md (domain list)
22+
*/
23+
24+
/**
25+
* UIPageControl on iOS 26.
26+
* @experimental
27+
*/
28+
export const pageControl = {
29+
/**
30+
* Diameter of one page indicator.
31+
* @source measured: UIKit lab, iOS 26.2 simulator, iPhone 17 Pro, a
32+
* 5-page control: indicator dots 9.67 pt (2026-10-23)
33+
*/
34+
indicatorSize: 9.67,
35+
/**
36+
* Space between neighbouring indicators.
37+
* @source measured: UIKit lab, iOS 26.2 simulator, iPhone 17 Pro, a
38+
* 5-page control: the indicator strip is 80.3 pt wide, 5 × 9.67 + 4 × 8
39+
* (2026-10-23)
40+
*/
41+
indicatorSpacing: 8,
42+
/**
43+
* Space between the strip and the control's leading and trailing edges.
44+
* @source measured: UIKit lab, iOS 26.2 simulator, iPhone 17 Pro, a
45+
* 5-page control 108.3 pt wide around an 80.3 pt strip (2026-10-23)
46+
*/
47+
horizontalPadding: 14,
48+
/**
49+
* The control's height.
50+
* @source measured: UIKit lab, iOS 26.2 simulator, iPhone 17 Pro,
51+
* intrinsicContentSize 108.3 × 25.67 at 5 pages and 161.3 × 25.67 at 50
52+
* (2026-10-23)
53+
*/
54+
height: 25.67,
55+
/**
56+
* The control is never wider than this, however many pages: past it the
57+
* indicators scroll in a window (size(forNumberOfPages: 50) is 903.3 pt).
58+
* @source measured: UIKit lab, iOS 26.2 simulator, iPhone 17 Pro,
59+
* intrinsicContentSize capped at 161.3 pt wide at 50 pages (2026-10-23)
60+
*/
61+
maxWidth: 161.3,
62+
/**
63+
* Opacity of the current page's indicator, of the indicator colour.
64+
* @source measured: UIKit lab, iOS 26.2 simulator, iPhone 17 Pro, the
65+
* default tint renders the current indicator white α 1 in light and dark
66+
* (#FFFFFF over black) (2026-10-23)
67+
*/
68+
currentIndicatorAlpha: 1,
69+
/**
70+
* Opacity of every other page's indicator, of the indicator colour.
71+
* @source measured: UIKit lab, iOS 26.2 simulator, iPhone 17 Pro, the
72+
* default tint renders the other indicators white α 0.45 in light and
73+
* dark (#737373 over black) (2026-10-23)
74+
*/
75+
otherIndicatorAlpha: 0.45,
76+
/**
77+
* A tap on the trailing side of the current page's indicator moves one
78+
* page forward, on the leading side one back; the change is reported when
79+
* the finger lifts.
80+
* @source measured: UIKit lab, iOS 26.2 simulator, iPhone 17 Pro, HID taps
81+
* on a 5-page control: valueChanged at the lift, about 70–90 ms after the
82+
* touch, one page per tap (2026-10-23)
83+
*/
84+
tapStep: 1,
85+
} as const;

‎packages/lab/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"devDependencies": {
7676
"@astryxdesign/a11y-spec": "workspace:*",
7777
"@astryxdesign/cli": "workspace:*",
78+
"@astryxdesign/interaction-harness": "workspace:*",
7879
"@babel/cli": "catalog:",
7980
"@babel/core": "catalog:",
8081
"@babel/preset-react": "catalog:",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/** @type {import('@astryxdesign/cli/authoring').ComponentDoc} */
4+
5+
export const docs = {
6+
name: 'PageControl',
7+
displayName: 'Page Control',
8+
category: 'Navigation',
9+
keywords: [
10+
'page control',
11+
'page indicator',
12+
'pager dots',
13+
'dots',
14+
'carousel dots',
15+
'onboarding',
16+
'paging',
17+
],
18+
description:
19+
'A row of page indicators under a paging view, as on iOS: one dot per page, the current one highlighted. A tap to either side of the current dot moves one page that way. Experimental: lands in lab first.',
20+
props: [
21+
{
22+
name: 'count',
23+
type: 'number',
24+
description: 'Number of pages.',
25+
required: true,
26+
},
27+
{
28+
name: 'value',
29+
type: 'number',
30+
description: 'The current page, 0-based.',
31+
required: true,
32+
},
33+
{
34+
name: 'onChange',
35+
type: '(page: number) => void',
36+
description:
37+
'Called with the page a tap, an arrow key, Home, End or an assistive technology asks for. Update the paging view and value together.',
38+
required: true,
39+
},
40+
{
41+
name: 'label',
42+
type: 'string',
43+
description:
44+
'Names the control for assistive technology. Defaults to "Page indicators", localized.',
45+
},
46+
{
47+
name: 'hidesForSinglePage',
48+
type: 'boolean',
49+
description: 'Render nothing when there is only one page.',
50+
default: 'false',
51+
},
52+
],
53+
usage: {
54+
description:
55+
'Use PageControl below a paging view, such as a Carousel with hasPaging or onboarding screens, to show how many pages there are and which one is showing. Keep its value in step with the view in both directions.',
56+
bestPractices: [
57+
{
58+
guidance: true,
59+
description:
60+
'Pair it with a view that pages by swipe: the control is a secondary way to move and the main way to see where you are.',
61+
},
62+
{
63+
guidance: true,
64+
description:
65+
'Keep pages few enough to count at a glance; past about ten the row stays a fixed width and scrolls.',
66+
},
67+
{
68+
guidance: false,
69+
description:
70+
"Don't use it for long lists of results (use Pagination) or for choosing between views (use SegmentedControl or TabList).",
71+
},
72+
],
73+
},
74+
};
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file PageControl.test.tsx
5+
* @input vitest (jsdom), Testing Library, PageControl
6+
* @output Proves the control's semantics (one named slider reading "Page N
7+
* of M"), its keyboard (arrows mirrored in RTL, Home, End), its measured
8+
* geometry and alphas, the width cap, hidesForSinglePage, and a tap's step
9+
* to either side of the current indicator
10+
* @position Testing; the real-touch proofs are
11+
* __tests__/PageControl.interaction.browser.spec.ts
12+
*/
13+
14+
import {describe, expect, it, vi} from 'vitest';
15+
import {fireEvent, render, screen} from '@testing-library/react';
16+
import {PageControl} from './PageControl';
17+
18+
function renderControl(
19+
props: Partial<React.ComponentProps<typeof PageControl>> = {},
20+
) {
21+
const onChange = vi.fn();
22+
const result = render(
23+
<PageControl count={5} value={2} onChange={onChange} {...props} />,
24+
);
25+
return {onChange, ...result};
26+
}
27+
28+
describe('PageControl', () => {
29+
it('is one slider named "Page indicators" that reads "Page 3 of 5"', () => {
30+
renderControl();
31+
const slider = screen.getByRole('slider', {name: 'Page indicators'});
32+
expect(slider).toHaveAttribute('aria-valuemin', '1');
33+
expect(slider).toHaveAttribute('aria-valuemax', '5');
34+
expect(slider).toHaveAttribute('aria-valuenow', '3');
35+
expect(slider).toHaveAttribute('aria-valuetext', 'Page 3 of 5');
36+
expect(slider).toHaveAttribute('tabindex', '0');
37+
});
38+
39+
it('takes a custom label', () => {
40+
renderControl({label: 'Onboarding'});
41+
expect(
42+
screen.getByRole('slider', {name: 'Onboarding'}),
43+
).toBeInTheDocument();
44+
});
45+
46+
it('arrow keys, Home and End ask for a page', () => {
47+
const {onChange} = renderControl();
48+
const slider = screen.getByRole('slider');
49+
fireEvent.keyDown(slider, {key: 'ArrowRight'});
50+
expect(onChange).toHaveBeenLastCalledWith(3);
51+
fireEvent.keyDown(slider, {key: 'ArrowLeft'});
52+
expect(onChange).toHaveBeenLastCalledWith(1);
53+
fireEvent.keyDown(slider, {key: 'ArrowUp'});
54+
expect(onChange).toHaveBeenLastCalledWith(3);
55+
fireEvent.keyDown(slider, {key: 'ArrowDown'});
56+
expect(onChange).toHaveBeenLastCalledWith(1);
57+
fireEvent.keyDown(slider, {key: 'Home'});
58+
expect(onChange).toHaveBeenLastCalledWith(0);
59+
fireEvent.keyDown(slider, {key: 'End'});
60+
expect(onChange).toHaveBeenLastCalledWith(4);
61+
});
62+
63+
it('mirrors the horizontal arrows in RTL', () => {
64+
const onChange = vi.fn();
65+
// jsdom does not inherit `direction`: set it on the control itself.
66+
render(
67+
<PageControl
68+
count={5}
69+
value={2}
70+
onChange={onChange}
71+
style={{direction: 'rtl'}}
72+
/>,
73+
);
74+
const slider = screen.getByRole('slider');
75+
fireEvent.keyDown(slider, {key: 'ArrowRight'});
76+
expect(onChange).toHaveBeenLastCalledWith(1);
77+
fireEvent.keyDown(slider, {key: 'ArrowLeft'});
78+
expect(onChange).toHaveBeenLastCalledWith(3);
79+
});
80+
81+
it('asks for nothing past the first or last page', () => {
82+
const {onChange} = renderControl({value: 4});
83+
fireEvent.keyDown(screen.getByRole('slider'), {key: 'ArrowRight'});
84+
expect(onChange).not.toHaveBeenCalled();
85+
});
86+
87+
it("draws UIPageControl's geometry: 9.67 px indicators, the current one opaque, the others at 45 %", () => {
88+
renderControl();
89+
const slider = screen.getByRole('slider');
90+
expect(slider.style.height).toBe('25.67px');
91+
expect(slider.style.width).toBe('108.35px');
92+
const indicators = Array.from(
93+
slider.firstElementChild?.children ?? [],
94+
) as HTMLElement[];
95+
expect(indicators).toHaveLength(5);
96+
expect(indicators[0].style.width).toBe('9.67px');
97+
expect(indicators.map(i => i.style.opacity)).toEqual([
98+
'0.45',
99+
'0.45',
100+
'1',
101+
'0.45',
102+
'0.45',
103+
]);
104+
});
105+
106+
it('never grows wider than 161.3 px; past that the row shifts to keep the current page in view', () => {
107+
renderControl({count: 50, value: 25});
108+
const slider = screen.getByRole('slider');
109+
expect(slider.style.width).toBe('161.3px');
110+
const strip = slider.firstElementChild as HTMLElement;
111+
expect(
112+
Number.parseFloat(strip.style.getPropertyValue('--_page-control-offset')),
113+
).toBeGreaterThan(0);
114+
});
115+
116+
it('with hidesForSinglePage, renders nothing for one page', () => {
117+
renderControl({count: 1, value: 0, hidesForSinglePage: true});
118+
expect(screen.queryByRole('slider')).toBeNull();
119+
});
120+
});

0 commit comments

Comments
 (0)