forked from bmcmahen/sancho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.tsx
227 lines (216 loc) · 5.91 KB
/
List.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/** @jsx jsx */
import { jsx } from "@emotion/core";
import * as React from "react";
import { Text } from "./Text";
import PropTypes from "prop-types";
import { MenuLabel } from "./Menu";
import { useTheme } from "./Theme/Providers";
import { noOp } from "./misc/noop";
import { OnPressFunction, useTouchable } from "touchable-hook";
import { safeBind } from "./Hooks/compose-bind";
export interface ListProps extends React.HTMLAttributes<HTMLDivElement> {
/** A series of ListItem elements */
children?: React.ReactNode;
}
export const List: React.FunctionComponent<ListProps> = ({
children,
...other
}) => {
return <nav {...other}>{children}</nav>;
};
List.propTypes = {
children: PropTypes.node
};
interface ListItemProps extends React.HTMLAttributes<any> {
onPress?: OnPressFunction;
component?: React.ReactType<any>;
/** An icon or avatar to appear to the left of the text content */
contentBefore?: React.ReactNode;
/** an icon to appear to the right of the text content */
contentAfter?: React.ReactNode;
/** whether the list item is interactive (ie., can be clicked as a button) */
interactive?: boolean;
/** optional third row of content */
children?: React.ReactNode;
/** whether primary and secondary text should be wrapped */
wrap?: boolean;
/** The primary text content of the list item */
primary: string | React.ReactNode;
/** the secondary text content */
secondary?: string | React.ReactNode;
[key: string]: any; // back hack to permit things like to='/page'
}
export const ListItem: React.FunctionComponent<ListItemProps> = ({
primary,
secondary,
contentBefore,
interactive = true,
children,
wrap = true,
contentAfter,
onPress = noOp,
component: Component = "div",
...other
}) => {
const interactiveProps = interactive
? {
role: "button",
tabIndex: 0
}
: {};
const isLink = other.to || other.href;
const { bind, hover, active } = useTouchable({
onPress,
behavior: isLink ? "link" : "button"
});
const theme = useTheme();
return (
<Component
className="ListItem"
css={[
{
display: "block",
textDecoration: "none",
outline: "none",
padding: theme.spaces.md,
background: "transparent",
WebkitTapHighlightColor: "transparent",
transition: "background 0.07s ease",
[theme.mediaQueries.md]: {
paddingLeft: theme.spaces.lg,
paddingRight: theme.spaces.lg
},
":last-child": {
borderBottom: "none"
}
},
interactive && {
cursor: "pointer",
":focus": {
boxShadow: `inset 0 0 3px ${theme.colors.text.selected}`
},
":focus:not([data-focus-visible-added])": {
boxShadow: "none"
}
},
interactive &&
hover && {
background: theme.colors.background.tint1
},
interactive &&
active && {
background: theme.colors.background.tint2
}
]}
{...interactiveProps}
{...safeBind(interactive ? bind : {}, other)}
>
<div
className="ListItem__container"
css={{ display: "flex", alignItems: "center" }}
>
{contentBefore && (
<div
className="ListItem__content-before"
css={{ marginRight: theme.spaces.md }}
>
{contentBefore}
</div>
)}
<div
className="ListItem__content"
css={{ flex: 1, overflow: "hidden" }}
>
<Text
className="ListItem__primary"
wrap={wrap}
variant="body"
css={{ display: "block", fontWeight: 500 }}
>
{primary}
</Text>
{secondary && (
<Text
className="ListItem__secondary"
wrap={wrap}
css={{ display: "block", fontSize: theme.fontSizes[0] }}
variant="body"
muted
>
{secondary}
</Text>
)}
{children}
</div>
{contentAfter && (
<div
className="ListItem__content-after"
css={{ flex: "0 0 auto", marginLeft: theme.spaces.md }}
>
{contentAfter}
</div>
)}
</div>
</Component>
);
};
ListItem.propTypes = {
primary: PropTypes.oneOfType([PropTypes.string, PropTypes.node]).isRequired,
secondary: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
contentBefore: PropTypes.node,
contentAfter: PropTypes.node,
wrap: PropTypes.bool,
children: PropTypes.node,
interactive: PropTypes.bool,
onPress: PropTypes.func,
component: PropTypes.elementType
};
interface ListSectionProps extends React.HTMLAttributes<HTMLDivElement> {
/** A title of the section */
title: string;
children?: React.ReactNode;
/** whether the title should stick to the top of the scrollable content */
sticky?: boolean;
}
export const ListSection: React.FunctionComponent<ListSectionProps> = ({
title,
children,
sticky = true,
...other
}) => {
const theme = useTheme();
const bg = theme.colors.background.layer;
return (
<div
css={{
":first-child > *": {
borderTop: "none"
}
}}
>
<MenuLabel
css={{
position: sticky ? "sticky" : "static",
top: 0,
backgroundColor: sticky ? bg : "transparent",
padding: theme.spaces.sm,
paddingLeft: theme.spaces.md,
marginTop: theme.spaces.md,
[theme.mediaQueries.md]: {
paddingLeft: theme.spaces.lg,
paddingRight: theme.spaces.lg
}
}}
{...other}
>
{title}
</MenuLabel>
{children}
</div>
);
};
ListSection.propTypes = {
title: PropTypes.string.isRequired,
sticky: PropTypes.bool,
children: PropTypes.node
};