react-aria-components 0.0.4 → 1.0.0-alpha.2
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/LICENSE +201 -0
- package/README.md +12 -29
- package/dist/import.mjs +5223 -0
- package/dist/main.js +5345 -0
- package/dist/main.js.map +1 -0
- package/dist/module.js +5223 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +1481 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +36 -44
- package/src/Breadcrumbs.tsx +90 -0
- package/src/Button.tsx +79 -0
- package/src/Calendar.tsx +471 -0
- package/src/Checkbox.tsx +240 -0
- package/src/Collection.tsx +787 -0
- package/src/ComboBox.tsx +129 -0
- package/src/DateField.tsx +244 -0
- package/src/DatePicker.tsx +178 -0
- package/src/Dialog.tsx +95 -0
- package/src/GridList.tsx +411 -0
- package/src/Group.tsx +69 -0
- package/src/Header.tsx +34 -0
- package/src/Heading.tsx +35 -0
- package/src/Input.tsx +73 -0
- package/src/Keyboard.tsx +24 -0
- package/src/Label.tsx +30 -0
- package/src/Link.tsx +98 -0
- package/src/ListBox.tsx +406 -0
- package/src/Menu.tsx +227 -0
- package/src/Meter.tsx +73 -0
- package/src/Modal.tsx +192 -0
- package/src/NumberField.tsx +79 -0
- package/src/OverlayArrow.tsx +70 -0
- package/src/Popover.tsx +125 -0
- package/src/ProgressBar.tsx +81 -0
- package/src/RadioGroup.tsx +219 -0
- package/src/SearchField.tsx +78 -0
- package/src/Select.tsx +180 -0
- package/src/Separator.tsx +53 -0
- package/src/Slider.tsx +217 -0
- package/src/Switch.tsx +127 -0
- package/src/Table.tsx +917 -0
- package/src/Tabs.tsx +282 -0
- package/src/Text.tsx +30 -0
- package/src/TextField.tsx +62 -0
- package/src/ToggleButton.tsx +59 -0
- package/src/Tooltip.tsx +135 -0
- package/src/index.ts +94 -0
- package/src/useDragAndDrop.tsx +172 -0
- package/src/utils.tsx +235 -0
- package/.babelrc +0 -17
- package/.eslintrc +0 -12
- package/src/accordion/accordion.css +0 -4
- package/src/accordion/accordion.js +0 -20
- package/src/accordion/index.js +0 -2
- package/src/accordion/section.css +0 -21
- package/src/accordion/section.js +0 -85
- package/src/examples/accordion-example.js +0 -73
- package/src/examples/example.js +0 -21
- package/src/examples/grid-cells/fancy-input-grid-cell.js +0 -206
- package/src/examples/grid-cells/input-grid-cell.js +0 -44
- package/src/examples/grid-example.css +0 -23
- package/src/examples/grid-example.js +0 -231
- package/src/examples/index.html +0 -10
- package/src/examples/index.js +0 -53
- package/src/examples/tabs-example.js +0 -52
- package/src/grid/column-header.css +0 -4
- package/src/grid/column-header.js +0 -29
- package/src/grid/grid-cell.css +0 -16
- package/src/grid/grid-cell.js +0 -104
- package/src/grid/grid-context.js +0 -3
- package/src/grid/grid.css +0 -4
- package/src/grid/grid.js +0 -110
- package/src/grid/index.js +0 -6
- package/src/grid/interactive-grid-cell.js +0 -96
- package/src/grid/row-headers.css +0 -6
- package/src/grid/row-headers.js +0 -22
- package/src/grid/row.css +0 -5
- package/src/grid/row.js +0 -21
- package/src/prop-types/ref.js +0 -3
- package/src/tabs/index.js +0 -4
- package/src/tabs/tab-list.css +0 -6
- package/src/tabs/tab-list.js +0 -103
- package/src/tabs/tab-panels.js +0 -35
- package/src/tabs/tab.css +0 -10
- package/src/tabs/tab.js +0 -45
- package/src/tabs/tabs.js +0 -52
- package/src/utils/debounce.js +0 -12
- package/src/utils/event-handlers-factory.js +0 -42
- package/src/utils/unique-id.js +0 -6
- package/webpack.config.js +0 -43
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 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 {
|
|
14
|
+
DropIndicatorProps as AriaDropIndicatorProps,
|
|
15
|
+
DraggableCollectionOptions,
|
|
16
|
+
DraggableItemProps,
|
|
17
|
+
DraggableItemResult,
|
|
18
|
+
DragItem,
|
|
19
|
+
DragPreview,
|
|
20
|
+
DropIndicatorAria,
|
|
21
|
+
DroppableCollectionOptions,
|
|
22
|
+
DroppableCollectionResult,
|
|
23
|
+
DroppableItemOptions,
|
|
24
|
+
DroppableItemResult,
|
|
25
|
+
DropTarget,
|
|
26
|
+
DropTargetDelegate,
|
|
27
|
+
ListDropTargetDelegate,
|
|
28
|
+
useDraggableCollection,
|
|
29
|
+
useDraggableItem,
|
|
30
|
+
useDropIndicator,
|
|
31
|
+
useDroppableCollection,
|
|
32
|
+
useDroppableItem
|
|
33
|
+
} from 'react-aria';
|
|
34
|
+
import {DraggableCollectionProps, DroppableCollectionProps} from '@react-types/shared';
|
|
35
|
+
import {
|
|
36
|
+
DraggableCollectionState,
|
|
37
|
+
DraggableCollectionStateOptions,
|
|
38
|
+
DroppableCollectionState,
|
|
39
|
+
DroppableCollectionStateOptions,
|
|
40
|
+
useDraggableCollectionState,
|
|
41
|
+
useDroppableCollectionState
|
|
42
|
+
} from 'react-stately';
|
|
43
|
+
import React, {createContext, ForwardedRef, forwardRef, Key, ReactNode, RefObject, useContext, useMemo} from 'react';
|
|
44
|
+
import {RenderProps} from './utils';
|
|
45
|
+
|
|
46
|
+
interface DraggableCollectionStateOpts extends Omit<DraggableCollectionStateOptions, 'getItems'> {}
|
|
47
|
+
|
|
48
|
+
interface DragHooks {
|
|
49
|
+
useDraggableCollectionState?: (props: DraggableCollectionStateOpts) => DraggableCollectionState,
|
|
50
|
+
useDraggableCollection?: (props: DraggableCollectionOptions, state: DraggableCollectionState, ref: RefObject<HTMLElement>) => void,
|
|
51
|
+
useDraggableItem?: (props: DraggableItemProps, state: DraggableCollectionState) => DraggableItemResult,
|
|
52
|
+
DragPreview?: typeof DragPreview,
|
|
53
|
+
renderDragPreview?: (items: DragItem[]) => JSX.Element
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface DropHooks {
|
|
57
|
+
useDroppableCollectionState?: (props: DroppableCollectionStateOptions) => DroppableCollectionState,
|
|
58
|
+
useDroppableCollection?: (props: DroppableCollectionOptions, state: DroppableCollectionState, ref: RefObject<HTMLElement>) => DroppableCollectionResult,
|
|
59
|
+
useDroppableItem?: (options: DroppableItemOptions, state: DroppableCollectionState, ref: RefObject<HTMLElement>) => DroppableItemResult,
|
|
60
|
+
useDropIndicator?: (props: AriaDropIndicatorProps, state: DroppableCollectionState, ref: RefObject<HTMLElement>) => DropIndicatorAria,
|
|
61
|
+
renderDropIndicator?: (target: DropTarget) => JSX.Element,
|
|
62
|
+
dropTargetDelegate?: DropTargetDelegate,
|
|
63
|
+
ListDropTargetDelegate: typeof ListDropTargetDelegate
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type DragAndDropHooks = DragHooks & DropHooks;
|
|
67
|
+
|
|
68
|
+
export interface DragAndDrop {
|
|
69
|
+
/** Drag and drop hooks for the collection element. */
|
|
70
|
+
dragAndDropHooks: DragAndDropHooks
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface DragAndDropOptions extends Omit<DraggableCollectionProps, 'preview' | 'getItems'>, DroppableCollectionProps {
|
|
74
|
+
/**
|
|
75
|
+
* A function that returns the items being dragged. If not specified, we assume that the collection is not draggable.
|
|
76
|
+
* @default () => []
|
|
77
|
+
*/
|
|
78
|
+
getItems?: (keys: Set<Key>) => DragItem[],
|
|
79
|
+
/**
|
|
80
|
+
* A function that renders a drag preview, which is shown under the user's cursor while dragging.
|
|
81
|
+
* By default, a copy of the dragged element is rendered.
|
|
82
|
+
*/
|
|
83
|
+
renderDragPreview?: (items: DragItem[]) => JSX.Element,
|
|
84
|
+
/**
|
|
85
|
+
* A function that renders a drop indicator element between two items in a collection.
|
|
86
|
+
* This should render a `<DropIndicator>` element. If this function is not provided, a
|
|
87
|
+
* default DropIndicator is provided.
|
|
88
|
+
*/
|
|
89
|
+
renderDropIndicator?: (target: DropTarget) => JSX.Element,
|
|
90
|
+
/** A custom delegate object that provides drop targets for pointer coordinates within the collection. */
|
|
91
|
+
dropTargetDelegate?: DropTargetDelegate
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Provides the hooks required to enable drag and drop behavior for a drag and drop compatible collection component.
|
|
96
|
+
*/
|
|
97
|
+
export function useDragAndDrop(options: DragAndDropOptions): DragAndDrop {
|
|
98
|
+
let dragAndDropHooks = useMemo(() => {
|
|
99
|
+
let {
|
|
100
|
+
onDrop,
|
|
101
|
+
onInsert,
|
|
102
|
+
onItemDrop,
|
|
103
|
+
onReorder,
|
|
104
|
+
onRootDrop,
|
|
105
|
+
getItems,
|
|
106
|
+
renderDragPreview,
|
|
107
|
+
renderDropIndicator,
|
|
108
|
+
dropTargetDelegate
|
|
109
|
+
} = options;
|
|
110
|
+
|
|
111
|
+
let isDraggable = !!getItems;
|
|
112
|
+
let isDroppable = !!(onDrop || onInsert || onItemDrop || onReorder || onRootDrop);
|
|
113
|
+
|
|
114
|
+
let hooks = {} as DragHooks & DropHooks;
|
|
115
|
+
if (isDraggable) {
|
|
116
|
+
hooks.useDraggableCollectionState = function useDraggableCollectionStateOverride(props: DraggableCollectionStateOpts) {
|
|
117
|
+
return useDraggableCollectionState({...props, ...options} as DraggableCollectionStateOptions);
|
|
118
|
+
};
|
|
119
|
+
hooks.useDraggableCollection = useDraggableCollection;
|
|
120
|
+
hooks.useDraggableItem = useDraggableItem;
|
|
121
|
+
hooks.DragPreview = DragPreview;
|
|
122
|
+
hooks.renderDragPreview = renderDragPreview;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (isDroppable) {
|
|
126
|
+
hooks.useDroppableCollectionState = function useDroppableCollectionStateOverride(props: DroppableCollectionStateOptions) {
|
|
127
|
+
return useDroppableCollectionState({...props, ...options});
|
|
128
|
+
},
|
|
129
|
+
hooks.useDroppableItem = useDroppableItem;
|
|
130
|
+
hooks.useDroppableCollection = function useDroppableCollectionOverride(props: DroppableCollectionOptions, state: DroppableCollectionState, ref: RefObject<HTMLElement>) {
|
|
131
|
+
return useDroppableCollection({...props, ...options}, state, ref);
|
|
132
|
+
};
|
|
133
|
+
hooks.useDropIndicator = useDropIndicator;
|
|
134
|
+
hooks.renderDropIndicator = renderDropIndicator;
|
|
135
|
+
hooks.dropTargetDelegate = dropTargetDelegate;
|
|
136
|
+
hooks.ListDropTargetDelegate = ListDropTargetDelegate;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return hooks;
|
|
140
|
+
}, [options]);
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
dragAndDropHooks
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export const DropIndicatorContext = createContext<DropIndicatorContextValue | null>(null);
|
|
148
|
+
|
|
149
|
+
export interface DropIndicatorRenderProps {
|
|
150
|
+
/**
|
|
151
|
+
* Whether the drop indicator is currently the active drop target.
|
|
152
|
+
* @selector [data-drop-target]
|
|
153
|
+
*/
|
|
154
|
+
isDropTarget: boolean
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface DropIndicatorProps extends AriaDropIndicatorProps, RenderProps<DropIndicatorRenderProps> {}
|
|
158
|
+
|
|
159
|
+
interface DropIndicatorContextValue {
|
|
160
|
+
render: (props: DropIndicatorProps, ref: ForwardedRef<HTMLElement>) => ReactNode
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function DropIndicator(props: DropIndicatorProps, ref: ForwardedRef<HTMLElement>): JSX.Element {
|
|
164
|
+
let {render} = useContext(DropIndicatorContext)!;
|
|
165
|
+
return <>{render(props, ref)}</>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A DropIndicator is rendered between items in a collection to indicate where dropped data will be inserted.
|
|
170
|
+
*/
|
|
171
|
+
const _DropIndicator = forwardRef(DropIndicator);
|
|
172
|
+
export {_DropIndicator as DropIndicator};
|
package/src/utils.tsx
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 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 {AriaLabelingProps, DOMProps as SharedDOMProps} from '@react-types/shared';
|
|
14
|
+
import {filterDOMProps, mergeProps, mergeRefs, useLayoutEffect, useObjectRef} from '@react-aria/utils';
|
|
15
|
+
import React, {createContext, CSSProperties, ReactNode, RefCallback, RefObject, useCallback, useContext, useEffect, useRef, useState} from 'react';
|
|
16
|
+
|
|
17
|
+
// Override forwardRef types so generics work.
|
|
18
|
+
declare function forwardRef<T, P = {}>(
|
|
19
|
+
render: (props: P, ref: React.Ref<T>) => React.ReactElement | null
|
|
20
|
+
): (props: P & React.RefAttributes<T>) => React.ReactElement | null;
|
|
21
|
+
|
|
22
|
+
export type forwardRefType = typeof forwardRef;
|
|
23
|
+
|
|
24
|
+
export const slotCallbackSymbol = Symbol('callback');
|
|
25
|
+
export const defaultSlot = Symbol('default');
|
|
26
|
+
|
|
27
|
+
interface SlottedValue<T> {
|
|
28
|
+
slots?: Record<string | symbol, T>,
|
|
29
|
+
[slotCallbackSymbol]?: (value: T) => void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type ContextValue<T extends SlotProps, E extends Element> = SlottedValue<WithRef<T, E>> | WithRef<T, E> | null | undefined;
|
|
33
|
+
|
|
34
|
+
type ProviderValue<T> = [React.Context<T>, T];
|
|
35
|
+
type ProviderValues<A, B, C, D, E, F, G, H> =
|
|
36
|
+
| [ProviderValue<A>]
|
|
37
|
+
| [ProviderValue<A>, ProviderValue<B>]
|
|
38
|
+
| [ProviderValue<A>, ProviderValue<B>, ProviderValue<C>]
|
|
39
|
+
| [ProviderValue<A>, ProviderValue<B>, ProviderValue<C>, ProviderValue<D>]
|
|
40
|
+
| [ProviderValue<A>, ProviderValue<B>, ProviderValue<C>, ProviderValue<D>, ProviderValue<E>]
|
|
41
|
+
| [ProviderValue<A>, ProviderValue<B>, ProviderValue<C>, ProviderValue<D>, ProviderValue<E>, ProviderValue<F>]
|
|
42
|
+
| [ProviderValue<A>, ProviderValue<B>, ProviderValue<C>, ProviderValue<D>, ProviderValue<E>, ProviderValue<F>, ProviderValue<G>]
|
|
43
|
+
| [ProviderValue<A>, ProviderValue<B>, ProviderValue<C>, ProviderValue<D>, ProviderValue<E>, ProviderValue<F>, ProviderValue<G>, ProviderValue<H>];
|
|
44
|
+
|
|
45
|
+
interface ProviderProps<A, B, C, D, E, F, G, H> {
|
|
46
|
+
values: ProviderValues<A, B, C, D, E, F, G, H>,
|
|
47
|
+
children: React.ReactNode
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function Provider<A, B, C, D, E, F, G, H>({values, children}: ProviderProps<A, B, C, D, E, F, G, H>): JSX.Element {
|
|
51
|
+
for (let [Context, value] of values) {
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
children = <Context.Provider value={value}>{children}</Context.Provider>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return children as JSX.Element;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface StyleProps {
|
|
60
|
+
/** The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. */
|
|
61
|
+
className?: string,
|
|
62
|
+
/** The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/Element/style) for the element. */
|
|
63
|
+
style?: CSSProperties
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface DOMProps extends StyleProps {
|
|
67
|
+
/** The children of the component. */
|
|
68
|
+
children?: ReactNode
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface StyleRenderProps<T> {
|
|
72
|
+
/** The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state. */
|
|
73
|
+
className?: string | ((values: T) => string),
|
|
74
|
+
/** The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/Element/style) for the element. A function may be provided to compute the style based on component state. */
|
|
75
|
+
style?: CSSProperties | ((values: T) => CSSProperties)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface RenderProps<T> extends StyleRenderProps<T> {
|
|
79
|
+
/** The children of the component. A function may be provided to alter the children based on component state. */
|
|
80
|
+
children?: ReactNode | ((values: T) => ReactNode)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface RenderPropsHookOptions<T> extends RenderProps<T>, SharedDOMProps, AriaLabelingProps {
|
|
84
|
+
values: T,
|
|
85
|
+
defaultChildren?: ReactNode,
|
|
86
|
+
defaultClassName?: string
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function useRenderProps<T>({className, style, children, defaultClassName, defaultChildren, values, ...otherProps}: RenderPropsHookOptions<T>) {
|
|
90
|
+
if (typeof className === 'function') {
|
|
91
|
+
className = className(values);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (typeof style === 'function') {
|
|
95
|
+
style = style(values);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (typeof children === 'function') {
|
|
99
|
+
children = children(values);
|
|
100
|
+
} else if (children == null) {
|
|
101
|
+
children = defaultChildren;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
delete otherProps.id;
|
|
105
|
+
return {
|
|
106
|
+
...filterDOMProps(otherProps),
|
|
107
|
+
className: className ?? defaultClassName,
|
|
108
|
+
style,
|
|
109
|
+
children
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type WithRef<T, E> = T & {ref?: React.ForwardedRef<E>};
|
|
114
|
+
export interface SlotProps {
|
|
115
|
+
/** A slot name for the component. Slots allow the component to receive props from a parent component. */
|
|
116
|
+
slot?: string
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function useContextProps<T, U, E extends Element>(props: T & SlotProps, ref: React.ForwardedRef<E>, context: React.Context<ContextValue<U, E>>): [T, React.RefObject<E>] {
|
|
120
|
+
let ctx = useContext(context) || {};
|
|
121
|
+
if ('slots' in ctx && ctx.slots) {
|
|
122
|
+
if (!props.slot && !ctx.slots[defaultSlot]) {
|
|
123
|
+
throw new Error('A slot prop is required');
|
|
124
|
+
}
|
|
125
|
+
let slot = props.slot || defaultSlot;
|
|
126
|
+
if (!ctx.slots[slot]) {
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
throw new Error(`Invalid slot "${props.slot}". Valid slot names are ` + new Intl.ListFormat().format(Object.keys(ctx.slots).map(p => `"${p}"`)) + '.');
|
|
129
|
+
}
|
|
130
|
+
ctx = ctx.slots[slot];
|
|
131
|
+
}
|
|
132
|
+
// @ts-ignore - TS says "Type 'unique symbol' cannot be used as an index type." but not sure why.
|
|
133
|
+
let {ref: contextRef, [slotCallbackSymbol]: callback, ...contextProps} = ctx;
|
|
134
|
+
let mergedRef = useObjectRef(mergeRefs(ref, contextRef));
|
|
135
|
+
let mergedProps = mergeProps(contextProps, props) as unknown as T;
|
|
136
|
+
|
|
137
|
+
// A parent component might need the props from a child, so call slot callback if needed.
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (callback) {
|
|
140
|
+
callback(props);
|
|
141
|
+
}
|
|
142
|
+
}, [callback, props]);
|
|
143
|
+
|
|
144
|
+
return [mergedProps, mergedRef];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function useSlot(): [RefCallback<Element>, boolean] {
|
|
148
|
+
// Assume we do have the slot in the initial render.
|
|
149
|
+
let [hasSlot, setHasSlot] = useState(true);
|
|
150
|
+
let hasRun = useRef(false);
|
|
151
|
+
|
|
152
|
+
// A callback ref which will run when the slotted element mounts.
|
|
153
|
+
// This should happen before the useLayoutEffect below.
|
|
154
|
+
let ref = useCallback(el => {
|
|
155
|
+
hasRun.current = true;
|
|
156
|
+
setHasSlot(!!el);
|
|
157
|
+
}, []);
|
|
158
|
+
|
|
159
|
+
// If the callback hasn't been called, then reset to false.
|
|
160
|
+
useLayoutEffect(() => {
|
|
161
|
+
if (!hasRun.current) {
|
|
162
|
+
setHasSlot(false);
|
|
163
|
+
}
|
|
164
|
+
}, []);
|
|
165
|
+
|
|
166
|
+
return [ref, hasSlot];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function useEnterAnimation(ref: RefObject<HTMLElement>, isReady: boolean = true) {
|
|
170
|
+
let [isEntering, setEntering] = useState(true);
|
|
171
|
+
useAnimation(ref, isEntering && isReady, useCallback(() => setEntering(false), []));
|
|
172
|
+
return isEntering && isReady;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function useExitAnimation(ref: RefObject<HTMLElement>, isOpen: boolean) {
|
|
176
|
+
// State to trigger a re-render after animation is complete, which causes the element to be removed from the DOM.
|
|
177
|
+
// Ref to track the state we're in, so we don't immediately reset isExiting to true after the animation.
|
|
178
|
+
let [isExiting, setExiting] = useState(false);
|
|
179
|
+
let exitState = useRef('idle');
|
|
180
|
+
|
|
181
|
+
// If isOpen becomes false, set isExiting to true.
|
|
182
|
+
if (!isOpen && ref.current && exitState.current === 'idle') {
|
|
183
|
+
isExiting = true;
|
|
184
|
+
setExiting(true);
|
|
185
|
+
exitState.current = 'exiting';
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// If we exited, and the element has been removed, reset exit state to idle.
|
|
189
|
+
if (!ref.current && exitState.current === 'exited') {
|
|
190
|
+
exitState.current = 'idle';
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
useAnimation(
|
|
194
|
+
ref,
|
|
195
|
+
isExiting,
|
|
196
|
+
useCallback(() => {
|
|
197
|
+
exitState.current = 'exited';
|
|
198
|
+
setExiting(false);
|
|
199
|
+
}, [])
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
return isExiting;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function useAnimation(ref: RefObject<HTMLElement>, isActive: boolean, onEnd: () => void) {
|
|
206
|
+
let prevAnimation = useRef<string | null>(null);
|
|
207
|
+
if (isActive && ref.current) {
|
|
208
|
+
prevAnimation.current = window.getComputedStyle(ref.current).animation;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
useLayoutEffect(() => {
|
|
212
|
+
if (isActive && ref.current) {
|
|
213
|
+
// Make sure there's actually an animation, and it wasn't there before we triggered the update.
|
|
214
|
+
let computedStyle = window.getComputedStyle(ref.current);
|
|
215
|
+
if (computedStyle.animationName !== 'none' && computedStyle.animation !== prevAnimation.current) {
|
|
216
|
+
let onAnimationEnd = (e: AnimationEvent) => {
|
|
217
|
+
if (e.target === ref.current) {
|
|
218
|
+
element.removeEventListener('animationend', onAnimationEnd);
|
|
219
|
+
onEnd();
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
let element = ref.current;
|
|
224
|
+
element.addEventListener('animationend', onAnimationEnd);
|
|
225
|
+
return () => {
|
|
226
|
+
element.removeEventListener('animationend', onAnimationEnd);
|
|
227
|
+
};
|
|
228
|
+
} else {
|
|
229
|
+
onEnd();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}, [ref, isActive, onEnd]);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export const HiddenContext = createContext<boolean>(false);
|
package/.babelrc
DELETED
package/.eslintrc
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "airbnb",
|
|
3
|
-
"rules": {
|
|
4
|
-
"import/extensions": "off",
|
|
5
|
-
"no-prototype-builtins": "off",
|
|
6
|
-
"object-curly-newline": "off",
|
|
7
|
-
"prefer-destructuring": "off",
|
|
8
|
-
"react/destructuring-assignment": "off",
|
|
9
|
-
"react/jsx-filename-extension": "off",
|
|
10
|
-
"react/jsx-one-expression-per-line": "off"
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import PropTypes from 'prop-types';
|
|
3
|
-
import styles from './accordion.css';
|
|
4
|
-
|
|
5
|
-
export default function Accordion({ children, className }) {
|
|
6
|
-
return (
|
|
7
|
-
<div className={className}>
|
|
8
|
-
{children}
|
|
9
|
-
</div>
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
Accordion.propTypes = {
|
|
14
|
-
children: PropTypes.node.isRequired,
|
|
15
|
-
className: PropTypes.string,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
Accordion.defaultProps = {
|
|
19
|
-
className: styles.container,
|
|
20
|
-
};
|
package/src/accordion/index.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
.container {
|
|
2
|
-
border-top: 1px solid LightGrey;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
.heading {
|
|
6
|
-
margin: 0;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
.panel {
|
|
10
|
-
border-top: 1px solid LightGrey;
|
|
11
|
-
padding: 1em 1.5em;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
.trigger {
|
|
15
|
-
background: none;
|
|
16
|
-
border: 0;
|
|
17
|
-
font-size: 1rem;
|
|
18
|
-
padding: 1em 1.5em;
|
|
19
|
-
text-align: left;
|
|
20
|
-
width: 100%
|
|
21
|
-
}
|
package/src/accordion/section.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import PropTypes from 'prop-types';
|
|
3
|
-
import uniqueId from '../utils/unique-id.js';
|
|
4
|
-
import styles from './section.css';
|
|
5
|
-
|
|
6
|
-
const headings = {
|
|
7
|
-
1: 'h1',
|
|
8
|
-
2: 'h2',
|
|
9
|
-
3: 'h3',
|
|
10
|
-
4: 'h4',
|
|
11
|
-
5: 'h5',
|
|
12
|
-
6: 'h6',
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export default class Section extends React.Component {
|
|
16
|
-
constructor() {
|
|
17
|
-
super();
|
|
18
|
-
this.accessibleId = uniqueId();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
render() {
|
|
22
|
-
const {
|
|
23
|
-
cannotClose,
|
|
24
|
-
children,
|
|
25
|
-
cssContainer,
|
|
26
|
-
cssHeading,
|
|
27
|
-
cssPanel,
|
|
28
|
-
cssTrigger,
|
|
29
|
-
headingLevel,
|
|
30
|
-
onClick,
|
|
31
|
-
open,
|
|
32
|
-
title,
|
|
33
|
-
} = this.props;
|
|
34
|
-
const titleVal = typeof title === 'function' ? title({ open }) : title;
|
|
35
|
-
const Heading = headings[headingLevel];
|
|
36
|
-
|
|
37
|
-
if (!headings.hasOwnProperty(headingLevel)) {
|
|
38
|
-
throw new Error('Accordion must have a headingLevel between 1 and 6');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return (
|
|
42
|
-
<div className={cssContainer}>
|
|
43
|
-
<Heading className={cssHeading}>
|
|
44
|
-
<button
|
|
45
|
-
aria-controls={this.accessibleId}
|
|
46
|
-
aria-disabled={cannotClose}
|
|
47
|
-
aria-expanded={open}
|
|
48
|
-
className={cssTrigger}
|
|
49
|
-
type="button"
|
|
50
|
-
onClick={onClick}
|
|
51
|
-
>
|
|
52
|
-
{titleVal}
|
|
53
|
-
</button>
|
|
54
|
-
</Heading>
|
|
55
|
-
<div className={open ? cssPanel : undefined} id={this.accessibleId}>
|
|
56
|
-
{open && children}
|
|
57
|
-
</div>
|
|
58
|
-
</div>
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
Section.propTypes = {
|
|
64
|
-
cannotClose: PropTypes.bool,
|
|
65
|
-
children: PropTypes.node.isRequired,
|
|
66
|
-
cssContainer: PropTypes.string,
|
|
67
|
-
cssHeading: PropTypes.string,
|
|
68
|
-
cssPanel: PropTypes.string,
|
|
69
|
-
cssTrigger: PropTypes.string,
|
|
70
|
-
headingLevel: PropTypes.oneOf([1, 2, 3, 4, 5, 6]).isRequired,
|
|
71
|
-
onClick: PropTypes.func,
|
|
72
|
-
open: PropTypes.bool,
|
|
73
|
-
title: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
Section.defaultProps = {
|
|
77
|
-
cannotClose: undefined,
|
|
78
|
-
cssContainer: styles.container,
|
|
79
|
-
cssHeading: styles.heading,
|
|
80
|
-
cssPanel: styles.panel,
|
|
81
|
-
cssTrigger: styles.trigger,
|
|
82
|
-
onClick: undefined,
|
|
83
|
-
open: false,
|
|
84
|
-
title: undefined,
|
|
85
|
-
};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import Example from './example.js';
|
|
3
|
-
import { Accordion, Section } from '../accordion';
|
|
4
|
-
|
|
5
|
-
function CustomTitle({ open, title }) { // eslint-disable-line react/prop-types
|
|
6
|
-
return (
|
|
7
|
-
<React.Fragment>
|
|
8
|
-
<span>{title}</span>
|
|
9
|
-
<span style={{ float: 'right' }}>{open ? '-' : '+'}</span>
|
|
10
|
-
</React.Fragment>
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default class AccordionExample extends React.Component {
|
|
15
|
-
constructor() {
|
|
16
|
-
super();
|
|
17
|
-
this.state = {
|
|
18
|
-
first: false,
|
|
19
|
-
second: false,
|
|
20
|
-
third: false,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
handleClick(which) {
|
|
25
|
-
return () => {
|
|
26
|
-
this.setState(state => ({
|
|
27
|
-
first: false,
|
|
28
|
-
second: false,
|
|
29
|
-
third: false,
|
|
30
|
-
[which]: !state[which],
|
|
31
|
-
}));
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
render() {
|
|
36
|
-
return (
|
|
37
|
-
<Example title="Accordion">
|
|
38
|
-
<Accordion>
|
|
39
|
-
<Section
|
|
40
|
-
headingLevel={3}
|
|
41
|
-
onClick={this.handleClick('first')}
|
|
42
|
-
open={this.state.first}
|
|
43
|
-
title={({ open }) => <CustomTitle open={open} title="First" />}
|
|
44
|
-
>
|
|
45
|
-
<span>
|
|
46
|
-
A
|
|
47
|
-
</span>
|
|
48
|
-
</Section>
|
|
49
|
-
<Section
|
|
50
|
-
headingLevel={3}
|
|
51
|
-
onClick={this.handleClick('second')}
|
|
52
|
-
open={this.state.second}
|
|
53
|
-
title={({ open }) => <CustomTitle open={open} title="Second" />}
|
|
54
|
-
>
|
|
55
|
-
<span>
|
|
56
|
-
B
|
|
57
|
-
</span>
|
|
58
|
-
</Section>
|
|
59
|
-
<Section
|
|
60
|
-
headingLevel={3}
|
|
61
|
-
onClick={this.handleClick('third')}
|
|
62
|
-
open={this.state.third}
|
|
63
|
-
title={({ open }) => <CustomTitle open={open} title="Third" />}
|
|
64
|
-
>
|
|
65
|
-
<span>
|
|
66
|
-
C
|
|
67
|
-
</span>
|
|
68
|
-
</Section>
|
|
69
|
-
</Accordion>
|
|
70
|
-
</Example>
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
}
|
package/src/examples/example.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import PropTypes from 'prop-types';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
|
|
4
|
-
export default function Example(props) {
|
|
5
|
-
return (
|
|
6
|
-
<section>
|
|
7
|
-
<header>
|
|
8
|
-
<h2>
|
|
9
|
-
{props.title}
|
|
10
|
-
</h2>
|
|
11
|
-
</header>
|
|
12
|
-
{props.children}
|
|
13
|
-
<hr />
|
|
14
|
-
</section>
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
Example.propTypes = {
|
|
19
|
-
children: PropTypes.node.isRequired,
|
|
20
|
-
title: PropTypes.node.isRequired,
|
|
21
|
-
};
|