Skip to content

Commit c4d476a

Browse files
jacobloganJacob Logan
andauthored
Docs collection (#2051)
* update collection docs, update existing examples, add grid example, add styling examples * use demo state and design tokens * add theming example to collection docs * add theming example and update styling examples * use design tokens and typo corrections * correct typo in docs Co-authored-by: Jacob Logan <[email protected]>
1 parent ac2a8cb commit c4d476a

17 files changed

+678
-233
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
CollectionProps,
3+
FlexProps,
4+
Flex,
5+
SelectField,
6+
TextField,
7+
} from '@aws-amplify/ui-react';
8+
import React from 'react';
9+
10+
export type CollectionPropControlsProps = Omit<
11+
CollectionProps<[]>,
12+
'type' | 'children' | 'items'
13+
> &
14+
FlexProps & {
15+
setDirection: (value: React.SetStateAction<FlexProps['direction']>) => void;
16+
setGap: (value: React.SetStateAction<FlexProps['gap']>) => void;
17+
setWrap: (value: React.SetStateAction<FlexProps['wrap']>) => void;
18+
};
19+
20+
interface CollectionPropControlsInterface {
21+
(props: CollectionPropControlsProps): JSX.Element;
22+
}
23+
24+
export const CollectionPropControls: CollectionPropControlsInterface = ({
25+
direction,
26+
setDirection,
27+
gap,
28+
setGap,
29+
wrap,
30+
setWrap,
31+
}) => {
32+
return (
33+
<Flex direction="column">
34+
<SelectField
35+
name="direction"
36+
id="direction"
37+
label="direction"
38+
value={direction as string}
39+
onChange={(event) => setDirection(event.target.value)}
40+
>
41+
<option value="row">row</option>
42+
<option value="column">column</option>
43+
</SelectField>
44+
45+
<TextField
46+
label="Gap"
47+
value={gap as string}
48+
onChange={(event) => setGap(event.target.value)}
49+
/>
50+
51+
<SelectField
52+
name="wrap"
53+
id="wrap"
54+
label="Wrap"
55+
value={wrap as string}
56+
onChange={(event) => setWrap(event.target.value)}
57+
>
58+
<option value="nowrap">nowrap</option>
59+
<option value="wrap">wrap</option>
60+
<option value="wrap-reverse">wrap-reverse</option>
61+
</SelectField>
62+
</Flex>
63+
);
64+
};
Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from 'react';
22
import {
33
Card,
4-
Text,
54
View,
65
Image,
76
Badge,
@@ -11,46 +10,55 @@ import {
1110
Heading,
1211
Collection,
1312
FlexContainerStyleProps,
13+
useTheme,
1414
} from '@aws-amplify/ui-react';
1515

16-
import { Example } from '@/components/Example';
17-
import { GetFieldControls } from '../shared/GetFieldControls';
18-
import { useFlexContainerStyleProps } from '../shared/useFlexContainerStyleProps';
16+
import { Demo } from '@/components/Demo';
17+
import { demoState } from '@/utils/demoState';
18+
import { CollectionPropControls } from './CollectionPropControls';
19+
import { useCollectionProps } from './useCollectionProps';
20+
import {
21+
filterDemoProps,
22+
getDemoProps,
23+
objectEntriesToPropString,
24+
} from '../utils/demoProps';
25+
26+
const propsToCode = (props) => {
27+
const filteredProps = filterDemoProps(props);
28+
return `
29+
<Collection
30+
type='list'
31+
${objectEntriesToPropString(Object.entries(filteredProps))}
32+
>
33+
{collectionItems}
34+
</Collection>`;
35+
};
1936

2037
export const CollectionDemo = () => {
21-
const flexStyleProps = useFlexContainerStyleProps({
22-
alignItems: 'normal',
23-
alignContent: 'start',
38+
const defaultValues = {
2439
direction: 'row',
2540
gap: '20px',
26-
justifyContent: 'space-between',
2741
wrap: 'nowrap',
28-
});
29-
const PropControls = GetFieldControls({
30-
typeName: 'Collection List (Flex)',
31-
fields: flexStyleProps,
32-
});
33-
const [
34-
[alignItems],
35-
[alignContent],
36-
[direction],
37-
[gap],
38-
[justifyContent],
39-
[wrap],
40-
] = flexStyleProps;
42+
};
43+
44+
const { tokens } = useTheme();
45+
46+
const collectionProps = useCollectionProps(
47+
demoState.get(Collection.displayName) || defaultValues
48+
);
49+
const demoProps = ['alignContent', 'alignItems', 'direction', 'gap', 'wrap'];
50+
const collectionDemoProps = getDemoProps(collectionProps, demoProps);
4151

4252
const list = [
4353
{
44-
title: 'Milford Sound - Room #1',
54+
title: 'Milford - Room #1',
4555
imageSrc: '/road-to-milford-new-zealand-800w.jpg',
4656
imageAlt:
4757
'Glittering stream with old log, snowy mountain peaks tower over a green field.',
48-
description:
49-
'Join us on this beautiful outdoor adventure through the glittering rivers through the snowy peaks on New Zealand.',
5058
badges: [
5159
{
5260
color: 'lightblue',
53-
text: 'Waterfront View',
61+
text: 'Waterfront',
5462
},
5563
{
5664
color: 'lightgreen',
@@ -59,16 +67,14 @@ export const CollectionDemo = () => {
5967
],
6068
},
6169
{
62-
title: 'Milford Sound - Room #2',
70+
title: 'Milford - Room #2',
6371
imageSrc: '/road-to-milford-new-zealand-800w.jpg',
6472
imageAlt:
6573
'Glittering stream with old log, snowy mountain peaks tower over a green field.',
66-
description:
67-
'Join us on this beautiful outdoor adventure through the glittering rivers through the snowy peaks on New Zealand.',
6874
badges: [
6975
{
70-
color: 'lightyellow',
71-
text: 'Mountain View',
76+
color: tokens.colors.yellow[60].value,
77+
text: 'Mountain',
7278
},
7379
{
7480
color: 'lightgreen',
@@ -79,42 +85,51 @@ export const CollectionDemo = () => {
7985
];
8086

8187
return (
82-
<View width="100%">
83-
{PropControls}
84-
<Divider margin="0.5rem 0.5rem" />
85-
<Example>
88+
<Demo
89+
code={propsToCode(collectionDemoProps)}
90+
propControls={<CollectionPropControls {...collectionProps} />}
91+
>
92+
<View
93+
backgroundColor={tokens.colors.background.secondary}
94+
padding={tokens.space.medium}
95+
>
8696
<Collection
8797
items={list}
8898
type="list"
89-
alignContent={alignContent as FlexContainerStyleProps['alignContent']}
90-
alignItems={alignItems as FlexContainerStyleProps['alignItems']}
91-
direction={direction as FlexContainerStyleProps['direction']}
92-
gap={gap as FlexContainerStyleProps['gap']}
93-
justifyContent={
94-
justifyContent as FlexContainerStyleProps['justifyContent']
99+
direction={
100+
collectionProps.direction as FlexContainerStyleProps['direction']
95101
}
96-
wrap={wrap as FlexContainerStyleProps['wrap']}
102+
gap={collectionProps.gap as FlexContainerStyleProps['gap']}
103+
wrap={collectionProps.wrap as FlexContainerStyleProps['wrap']}
97104
>
98105
{(item, index) => (
99-
<Card key={index}>
106+
<Card
107+
key={index}
108+
maxWidth="20rem"
109+
variation="outlined"
110+
borderRadius="5px"
111+
>
100112
<Image src={item.imageSrc} alt={item.imageAlt} />
101-
<View padding="1rem">
113+
<View padding={tokens.space.xs}>
102114
<Flex>
103115
{item.badges.map((badge) => (
104116
<Badge key={badge.text} backgroundColor={badge.color}>
105117
{badge.text}
106118
</Badge>
107119
))}
108120
</Flex>
109-
<Divider padding="1rem 0 0 0" />
110-
<Heading level={3}>{item.title}</Heading>
111-
<Text padding="0 0 1rem 0">{item.description}</Text>
112-
<Button isFullWidth={true}>Book it</Button>
121+
<Divider padding={`${tokens.space.xxxs} 0`} />
122+
<Heading level={6} padding={tokens.space.xxxs}>
123+
{item.title}
124+
</Heading>
125+
<Button isFullWidth={true} variation="primary">
126+
Book it
127+
</Button>
113128
</View>
114129
</Card>
115130
)}
116131
</Collection>
117-
</Example>
118-
</View>
132+
</View>
133+
</Demo>
119134
);
120135
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Collection, Button, useTheme } from '@aws-amplify/ui-react';
2+
import { countries } from 'countries-list';
3+
4+
export const CollectionStylePropExample = () => {
5+
const { tokens } = useTheme();
6+
const items = Object.values(countries)
7+
.map(({ name, emoji }) => ({
8+
name,
9+
emoji,
10+
}))
11+
.splice(0, 20);
12+
return (
13+
<Collection
14+
type="list"
15+
border={`${tokens.borderWidths.medium} solid ${tokens.colors.red[60]}`}
16+
direction="row"
17+
wrap="wrap"
18+
padding="5px"
19+
gap="20px"
20+
items={items}
21+
>
22+
{(regions, index) => (
23+
<Button grow="1" key={index}>
24+
{regions.emoji}
25+
</Button>
26+
)}
27+
</Collection>
28+
);
29+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
Button,
3+
Collection,
4+
defaultDarkModeOverride,
5+
ThemeProvider,
6+
Theme,
7+
} from '@aws-amplify/ui-react';
8+
import { countries } from 'countries-list';
9+
10+
export const CollectionThemeExample = () => {
11+
const theme: Theme = {
12+
name: 'collection-theme',
13+
tokens: {
14+
components: {
15+
collection: {
16+
search: {
17+
button: {
18+
color: { value: '{colors.brand.secondary.40}' },
19+
_active: {
20+
backgroundColor: { value: '{colors.brand.primary.80}' },
21+
},
22+
_focus: {
23+
backgroundColor: { value: '{colors.brand.primary.80}' },
24+
},
25+
_hover: {
26+
backgroundColor: { value: '{colors.brand.primary.80}' },
27+
},
28+
},
29+
},
30+
},
31+
},
32+
},
33+
overrides: [defaultDarkModeOverride],
34+
};
35+
return (
36+
<ThemeProvider theme={theme} colorMode="system">
37+
<Collection
38+
type="grid"
39+
templateColumns="1fr 1fr 1fr"
40+
gap="15px"
41+
items={Object.values(countries).map(({ name, emoji }) => ({
42+
name,
43+
emoji,
44+
}))}
45+
isSearchable
46+
isPaginated
47+
itemsPerPage={9}
48+
searchPlaceholder="Type to search..."
49+
searchFilter={(regions, keyword) =>
50+
(regions as any).name.toLowerCase().startsWith(keyword.toLowerCase())
51+
}
52+
>
53+
{(regions, index) => (
54+
<Button grow="1" key={index}>
55+
{regions.emoji} {regions.name}
56+
</Button>
57+
)}
58+
</Collection>
59+
</ThemeProvider>
60+
);
61+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Collection, Card, Heading, Text } from '@aws-amplify/ui-react';
2+
3+
export const DefaultCollectionExample = () => {
4+
return (
5+
<Collection
6+
type="list"
7+
items={[
8+
{
9+
title: 'Fiordland National Park',
10+
description:
11+
'This national park includes the famous fjords of Milford, Dusky and Doubtful Sounds.',
12+
},
13+
{
14+
title: 'Bay of Islands, North Island',
15+
description:
16+
'Three hours north of Auckland, this area features over 144 islands to explore.',
17+
},
18+
{
19+
title: 'Queenstown, South Island',
20+
description:
21+
"This hopping town is New Zealand's adventure capital and is located right on Lake Wakatipu.",
22+
},
23+
]}
24+
gap="1.5rem"
25+
>
26+
{(item, index) => (
27+
<Card key={index} padding="1rem">
28+
<Heading level={4}>{item.title}</Heading>
29+
<Text>{item.description}</Text>
30+
</Card>
31+
)}
32+
</Collection>
33+
);
34+
};

0 commit comments

Comments
 (0)