forked from Thinkmill/keystatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionBar.tsx
202 lines (185 loc) · 6.1 KB
/
ActionBar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { FocusScope } from '@react-aria/focus';
import { announce } from '@react-aria/live-announcer';
import { useLocalizedStringFormatter } from '@react-aria/i18n';
import { useKeyboard } from '@react-aria/interactions';
import { filterDOMProps, useObjectRef } from '@react-aria/utils';
import React, {
ForwardedRef,
ReactElement,
Ref,
useEffect,
useRef,
useState,
} from 'react';
import { ActionGroup } from '@keystar/ui/action-group';
import { ActionButton } from '@keystar/ui/button';
import { Icon } from '@keystar/ui/icon';
import { xIcon } from '@keystar/ui/icon/icons/xIcon';
import { Transition } from '@keystar/ui/overlays';
import {
classNames,
css,
tokenSchema,
transition,
useStyleProps,
} from '@keystar/ui/style';
import { Text } from '@keystar/ui/typography';
import { useProviderProps } from '@keystar/ui/core';
import localizedMessages from './l10n';
import { ActionBarProps } from './types';
import { actionbarClassList } from './class-list';
function ActionBar<T extends object>(
props: ActionBarProps<T>,
forwardedRef: ForwardedRef<HTMLDivElement>
) {
let isOpen = props.selectedItemCount !== 0;
let domRef = useObjectRef(forwardedRef);
return (
<Transition nodeRef={domRef} isOpen={isOpen}>
<ActionBarInnerWithRef {...props} ref={domRef} />
</Transition>
);
}
interface ActionBarInnerProps<T> extends ActionBarProps<T> {
isOpen?: boolean;
}
function ActionBarInner<T>(
props: ActionBarInnerProps<T>,
ref: Ref<HTMLDivElement>
) {
props = useProviderProps(props);
let {
children,
onAction,
onClearSelection,
selectedItemCount,
isOpen,
items,
buttonLabelBehavior,
} = props;
let styleProps = useStyleProps(props);
let stringFormatter = useLocalizedStringFormatter(localizedMessages);
// Store the last count greater than zero in a ref so that we can retain it while rendering the fade-out animation.
let [lastCount, setLastCount] = useState(selectedItemCount);
if (
(selectedItemCount === 'all' || selectedItemCount > 0) &&
selectedItemCount !== lastCount
) {
setLastCount(selectedItemCount);
}
let { keyboardProps } = useKeyboard({
onKeyDown(e) {
if (e.key === 'Escape') {
e.preventDefault();
onClearSelection();
}
},
});
// Announce "actions available" on mount.
let isInitial = useRef(true);
useEffect(() => {
if (isInitial.current) {
isInitial.current = false;
announce(stringFormatter.format('actionsAvailable'));
}
}, [stringFormatter]);
// FIXME: style props are passed to both the root and the bar elements
return (
<FocusScope restoreFocus>
<div
{...filterDOMProps(props)}
{...styleProps}
{...keyboardProps}
data-open={isOpen}
ref={ref}
className={classNames(
css({
flex: 'none',
height: 0,
opacity: 0,
overflow: 'hidden',
transition: transition(['height', 'opacity'], {
duration: 'short',
}),
'&[data-open="true"]': {
height: `calc(${tokenSchema.size.element.large} + (2 * ${tokenSchema.size.space.regular}))`,
opacity: 1,
},
}),
actionbarClassList.element('root'),
styleProps.className
)}
>
<div
data-open={isOpen}
className={classNames(
css({
alignItems: 'center',
backgroundColor: tokenSchema.color.background.canvas,
border: `${tokenSchema.size.border.regular} solid ${tokenSchema.color.border.emphasis}`,
borderRadius: tokenSchema.size.radius.regular,
boxShadow: `0 1px 4px ${tokenSchema.color.shadow.regular}`,
display: 'grid',
gap: tokenSchema.size.space.small,
gridTemplateAreas: '"clear selected . actiongroup"',
gridTemplateColumns: `auto max-content minmax(${tokenSchema.size.element.small}, 1fr) auto`,
bottom: tokenSchema.size.space.regular,
insetInline: tokenSchema.size.space.regular,
isolation: 'isolate',
justifyContent: 'space-between',
margin: '0 auto',
padding: tokenSchema.size.space.regular,
position: 'absolute',
transform: `translateY(${tokenSchema.size.space.large})`, // initialise with offset
transition: transition('transform', { duration: 'short' }),
'&[data-open="true"]': {
transform: 'translateY(0)',
},
}),
actionbarClassList.element('bar')
)}
>
<ActionGroup
items={items}
aria-label={stringFormatter.format('actions')}
prominence="low"
overflowMode="collapse"
buttonLabelBehavior={buttonLabelBehavior}
onAction={onAction}
gridArea="actiongroup"
>
{children}
</ActionGroup>
<ActionButton
gridArea="clear"
aria-label={stringFormatter.format('clearSelection')}
onPress={() => onClearSelection()}
prominence="low"
>
<Icon src={xIcon} />
</ActionButton>
<Text gridArea="selected">
{lastCount === 'all'
? stringFormatter.format('selectedAll')
: `${lastCount} selected`}
{/*
FIXME: resolve @react-aria/i18n issues
stringFormatter.format('selected', { count: lastCount })
*/}
</Text>
</div>
</div>
</FocusScope>
);
}
const ActionBarInnerWithRef = React.forwardRef(ActionBarInner) as <T>(
props: ActionBarInnerProps<T> & { ref?: Ref<HTMLDivElement> }
) => ReactElement;
/**
* Action bars are used for single and bulk selection patterns when a user needs
* to perform actions on one or more items at the same time.
*/
const _ActionBar: <T>(
props: ActionBarProps<T> & { ref?: ForwardedRef<HTMLDivElement> }
) => ReactElement = React.forwardRef(ActionBar as any) as any;
export { _ActionBar as ActionBar };