Skip to content

Commit 2f32c05

Browse files
authored
Add docs for contexts in React Aria Components (#4960)
1 parent 8d367e0 commit 2f32c05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2971
-415
lines changed

packages/@react-aria/interactions/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type {HoverProps, HoverResult} from './useHover';
3737
export type {InteractOutsideProps} from './useInteractOutside';
3838
export type {KeyboardProps, KeyboardResult} from './useKeyboard';
3939
export type {PressProps, PressHookProps, PressResult} from './usePress';
40-
export type {MoveEvents} from '@react-types/shared';
40+
export type {PressEvent, PressEvents, MoveStartEvent, MoveMoveEvent, MoveEndEvent, MoveEvents, HoverEvent, HoverEvents, FocusEvents, KeyboardEvents} from '@react-types/shared';
4141
export type {MoveResult} from './useMove';
4242
export type {LongPressProps, LongPressResult} from './useLongPress';
4343
export type {ScrollWheelProps} from './useScrollWheel';

packages/@react-aria/slider/src/useSlider.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import {AriaSliderProps} from '@react-types/slider';
1414
import {clamp, mergeProps, useGlobalListeners} from '@react-aria/utils';
1515
import {DOMAttributes} from '@react-types/shared';
16-
import {getSliderThumbId, sliderIds} from './utils';
16+
import {getSliderThumbId, sliderData} from './utils';
1717
import React, {LabelHTMLAttributes, OutputHTMLAttributes, RefObject, useRef} from 'react';
1818
import {setInteractionModality, useMove} from '@react-aria/interactions';
1919
import {SliderState} from '@react-stately/slider';
@@ -54,7 +54,11 @@ export function useSlider<T extends number | number[]>(
5454
let isVertical = props.orientation === 'vertical';
5555

5656
// Attach id of the label to the state so it can be accessed by useSliderThumb.
57-
sliderIds.set(state, labelProps.id ?? fieldProps.id);
57+
sliderData.set(state, {
58+
id: labelProps.id ?? fieldProps.id,
59+
'aria-describedby': props['aria-describedby'],
60+
'aria-details': props['aria-details']
61+
});
5862

5963
let {direction} = useLocale();
6064

packages/@react-aria/slider/src/useSliderThumb.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {AriaSliderThumbProps} from '@react-types/slider';
22
import {clamp, focusWithoutScrolling, mergeProps, useFormReset, useGlobalListeners} from '@react-aria/utils';
33
import {DOMAttributes} from '@react-types/shared';
4-
import {getSliderThumbId, sliderIds} from './utils';
4+
import {getSliderThumbId, sliderData} from './utils';
55
import React, {ChangeEvent, InputHTMLAttributes, LabelHTMLAttributes, RefObject, useCallback, useEffect, useRef} from 'react';
66
import {SliderState} from '@react-stately/slider';
77
import {useFocusable} from '@react-aria/focus';
@@ -60,11 +60,11 @@ export function useSliderThumb(
6060
let {direction} = useLocale();
6161
let {addGlobalListener, removeGlobalListener} = useGlobalListeners();
6262

63-
let labelId = sliderIds.get(state);
63+
let data = sliderData.get(state);
6464
const {labelProps, fieldProps} = useLabel({
6565
...opts,
6666
id: getSliderThumbId(state, index),
67-
'aria-labelledby': `${labelId} ${opts['aria-labelledby'] ?? ''}`.trim()
67+
'aria-labelledby': `${data.id} ${opts['aria-labelledby'] ?? ''}`.trim()
6868
});
6969

7070
const value = state.values[index];
@@ -247,6 +247,8 @@ export function useSliderThumb(
247247
'aria-required': isRequired || undefined,
248248
'aria-invalid': validationState === 'invalid' || undefined,
249249
'aria-errormessage': opts['aria-errormessage'],
250+
'aria-describedby': [data['aria-describedby'], opts['aria-describedby']].filter(Boolean).join(' '),
251+
'aria-details': [data['aria-details'], opts['aria-details']].filter(Boolean).join(' '),
250252
onChange: (e: ChangeEvent<HTMLInputElement>) => {
251253
state.setThumbValue(index, parseFloat(e.target.value));
252254
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import {SliderState} from '@react-stately/slider';
22

3-
export const sliderIds = new WeakMap<SliderState, string>();
3+
interface SliderData {
4+
id: string,
5+
'aria-describedby'?: string,
6+
'aria-details'?: string
7+
}
8+
9+
export const sliderData = new WeakMap<SliderState, SliderData>();
410

511
export function getSliderThumbId(state: SliderState, index: number) {
6-
let id = sliderIds.get(state);
7-
if (!id) {
12+
let data = sliderData.get(state);
13+
if (!data) {
814
throw new Error('Unknown slider state');
915
}
1016

11-
return `${id}-${index}`;
17+
return `${data.id}-${index}`;
1218
}

packages/@react-aria/tabs/docs/useTabList.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function Example() {
236236
The Jurassic ranges from 200 million years to 145 million years ago.
237237
</Item>
238238
<Item key="cretaceous" title="Cretaceous">
239-
The Cretaceous is the longest period of the Mesozoic, spanning from 145 million to 66 years ago.
239+
The Cretaceous is the longest period of the Mesozoic, spanning from 145 million to 66 million years ago.
240240
</Item>
241241
</Tabs>
242242
</>

packages/@react-spectrum/tabs/docs/Tabs.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function Example() {
271271
let tabs = [
272272
{name: 'Triassic', children: 'The Triassic ranges roughly from 252 million to 201 million years ago, preceding the Jurassic Period.'},
273273
{name: 'Jurassic', children: 'The Jurassic ranges from 200 million years to 145 million years ago.'},
274-
{name: 'Cretaceous', children: 'The Cretaceous is the longest period of the Mesozoic, spanning from 145 million to 66 years ago.'}
274+
{name: 'Cretaceous', children: 'The Cretaceous is the longest period of the Mesozoic, spanning from 145 million to 66 million years ago.'}
275275
];
276276
type Tab = typeof tabs[0];
277277
let [timePeriod, setTimePeriod] = React.useState<React.Key>('Triassic');
@@ -310,7 +310,7 @@ function Example() {
310310
let tabs = [
311311
{name: 'Triassic', children: 'The Triassic ranges roughly from 252 million to 201 million years ago, preceding the Jurassic Period.'},
312312
{name: 'Jurassic', children: 'The Jurassic ranges from 200 million years to 145 million years ago.'},
313-
{name: 'Cretaceous', children: 'The Cretaceous is the longest period of the Mesozoic, spanning from 145 million to 66 years ago.'}
313+
{name: 'Cretaceous', children: 'The Cretaceous is the longest period of the Mesozoic, spanning from 145 million to 66 million years ago.'}
314314
];
315315
type Tab = typeof tabs[0];
316316

packages/dev/docs/src/ContextTable.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2020 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import clsx from 'clsx';
14+
import React from 'react';
15+
import styles from './docs.css';
16+
import tableStyles from '@adobe/spectrum-css-temp/components/table/vars.css';
17+
import {Type, TypeContext} from './types';
18+
import {TypeLink} from './TypeLink';
19+
import typographyStyles from '@adobe/spectrum-css-temp/components/typography/vars.css';
20+
21+
export function ContextTable({components, docs}) {
22+
return (
23+
<table className={`${tableStyles['spectrum-Table']} ${styles.propTable}`}>
24+
<thead>
25+
<tr>
26+
<td role="columnheader" className={tableStyles['spectrum-Table-headCell']}>Component</td>
27+
<td role="columnheader" className={tableStyles['spectrum-Table-headCell']}>Context</td>
28+
<td role="columnheader" className={tableStyles['spectrum-Table-headCell']}>Props</td>
29+
<td role="columnheader" className={tableStyles['spectrum-Table-headCell']}>Ref</td>
30+
</tr>
31+
</thead>
32+
<tbody className={tableStyles['spectrum-Table-body']}>
33+
{components.map((comp, index) => (
34+
<tr key={index} className={clsx(tableStyles['spectrum-Table-row'], styles.tableRow)}>
35+
<td role="rowheader" className={clsx(tableStyles['spectrum-Table-cell'], styles.tableCell)} data-column="Component">
36+
<code className={`${typographyStyles['spectrum-Code4']}`}>
37+
<span className="token hljs-variable">{comp}</span>
38+
</code>
39+
</td>
40+
<td role="rowheader" className={clsx(tableStyles['spectrum-Table-cell'], styles.tableCell)} data-column="Context">
41+
<code className={`${typographyStyles['spectrum-Code4']}`}>
42+
<span className="token hljs-variable">{`${comp}Context`}</span>
43+
</code>
44+
</td>
45+
<td role="rowheader" className={clsx(tableStyles['spectrum-Table-cell'], styles.tableCell)} data-column="Props">
46+
<TypeLink links={docs.links} type={docs.exports[comp].props} />
47+
</td>
48+
<td role="rowheader" className={clsx(tableStyles['spectrum-Table-cell'], styles.tableCell)} data-column="Ref">
49+
<TypeContext.Provider value={docs.links}>
50+
<code className={`${typographyStyles['spectrum-Code4']}`}>
51+
<Type type={docs.exports[comp].ref.typeParameters[0]} />
52+
</code>
53+
</TypeContext.Provider>
54+
</td>
55+
</tr>
56+
))}
57+
</tbody>
58+
</table>
59+
);
60+
}

packages/dev/docs/src/PropTable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ export function PropTable({component, links, style}) {
8181
return (
8282
<div style={style}>
8383
<TypeContext.Provider value={links}>
84-
<InterfaceType properties={ungrouped} showRequired showDefault isComponent name={component.name} />
84+
<InterfaceType properties={ungrouped} showRequired isComponent name={component.name} />
8585
{Object.keys(groups).map((group, i) => (
8686
<details key={i}>
8787
<summary className={typographyStyles['spectrum-Heading4']}>
8888
<ChevronRight size="S" />
8989
{group}
9090
</summary>
91-
<InterfaceType properties={groups[group]} showRequired showDefault isComponent name={component.name} />
91+
<InterfaceType properties={groups[group]} showRequired isComponent name={component.name} />
9292
</details>
9393
))}
9494
</TypeContext.Provider>

packages/dev/docs/src/StateTable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function StateTable({properties, showOptional}) {
2424
}
2525

2626
return (
27-
<table className={`${tableStyles['spectrum-Table']} ${tableStyles['spectrum-Table--quiet']} ${styles.propTable}`}>
27+
<table className={`${tableStyles['spectrum-Table']} ${styles.propTable}`}>
2828
<thead>
2929
<tr>
3030
<td role="columnheader" className={tableStyles['spectrum-Table-headCell']}>Name</td>

packages/dev/docs/src/docs.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ footer li:last-child:after {
506506

507507
.article pre {
508508
background: var(--spectrum-global-color-gray-75);
509+
border: 1px solid var(--spectrum-gray-200);
509510
--code-padding: 18px;
510511
padding: var(--code-padding);
511512
border-radius: 4px;
@@ -515,6 +516,7 @@ footer li:last-child:after {
515516

516517
.article pre:global(.example) {
517518
border-radius: 4px 4px 0 0;
519+
border-bottom: none;
518520
margin-bottom: 0;
519521
}
520522

@@ -526,6 +528,8 @@ footer li:last-child:after {
526528

527529
.article pre + div > .example {
528530
border-radius: 0 0 4px 4px;
531+
border: 1px solid var(--spectrum-gray-200);
532+
border-top: none;
529533
}
530534

531535
code {

0 commit comments

Comments
 (0)