Skip to content

Commit bc56b76

Browse files
siddharthkpSaraVieira
authored andcommitted
components: Element (#3287)
* remove duplicate decorator * add parent styles to layout decorator * add min height to layout decorator * add common base element * use base element for other components * add all fontsizes + update example
1 parent 2162cb7 commit bc56b76

File tree

11 files changed

+75
-27
lines changed

11 files changed

+75
-27
lines changed

packages/common/src/design-language/typography.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ export const fontSizes = [
44
11,
55
13,
66
16,
7+
19,
8+
24,
9+
28,
10+
33,
11+
40,
712
];
813

914
export const fontWeights = {

packages/components/.storybook/decorators/LayoutDecorator.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,29 @@ import React from 'react';
33
import styled from 'styled-components';
44

55
const ColoredChildren = styled.div`
6-
& > div > * {
6+
& > * {
7+
background: #343434;
8+
min-height: 4em;
9+
}
10+
& > * > * {
711
--color: rgb(103, 126, 208);
812
background: var(--color);
913
min-height: 4em;
1014
min-width: 4em;
1115
}
12-
& > div > *:nth-child(6n + 2) {
16+
& > * > *:nth-child(6n + 2) {
1317
--color: rgb(217, 103, 219);
1418
}
15-
& > div > *:nth-child(6n + 3) {
19+
& > * > *:nth-child(6n + 3) {
1620
--color: rgb(77, 214, 115);
1721
}
18-
& > div > *:nth-child(6n + 4) {
22+
& > * > *:nth-child(6n + 4) {
1923
--color: rgb(248, 110, 91);
2024
}
21-
& > div > *:nth-child(6n + 5) {
25+
& > * > *:nth-child(6n + 5) {
2226
--color: rgb(94, 204, 211);
2327
}
24-
& > div > *:nth-child(6n + 6) {
28+
& > * > *:nth-child(6n + 6) {
2529
--color: rgb(0, 35, 208);
2630
}
2731
& > div > *:nth-child(6n + 7) {

packages/components/src/components/Collapsible/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import styled from 'styled-components';
33
import css from '@styled-system/css';
4+
import { Element } from '../Element';
45

56
export const Header = styled.div(
67
css({
@@ -81,12 +82,12 @@ export const Collapsible: React.FC<ICollapsibleProps> = ({
8182
const toggle = () => setOpen(!open);
8283

8384
return (
84-
<section {...props}>
85+
<Element as="section" {...props}>
8586
<Header onClick={toggle}>
8687
<ToggleIcon open={open} />
8788
<Title>{title}</Title>
8889
</Header>
8990
{open ? <Body>{children}</Body> : null}
90-
</section>
91+
</Element>
9192
);
9293
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import { LayoutDecorator } from '../../../.storybook/decorators';
3+
import { Element } from '.';
4+
5+
export default {
6+
title: 'components/Element',
7+
component: Element,
8+
decorators: [LayoutDecorator],
9+
};
10+
11+
export const Basic = () => <Element>content</Element>;
12+
13+
export const AsProp = () => <Element as="span">content</Element>;
14+
15+
export const Margins = () => (
16+
<>
17+
<Element margin={2}>2 on the space grid is 2*4px = 8px or 0.5em</Element>
18+
<Element marginX={2}>left and right</Element>
19+
<Element marginY={2}>top and bottom</Element>
20+
<Element marginBottom={2}>prefer margin bottom when you can</Element>
21+
</>
22+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import styled from 'styled-components';
2+
import css from '@styled-system/css';
3+
4+
export const Element = styled.div<{
5+
margin?: number;
6+
marginX?: number;
7+
marginY?: number;
8+
marginBottom?: number;
9+
marginTop?: number; // prefer margin bottom to top
10+
}>(props =>
11+
css({
12+
margin: props.margin || null,
13+
marginX: props.marginX || null,
14+
marginY: props.marginY || null,
15+
marginBottom: props.marginBottom || null,
16+
marginTop: props.marginTop || null,
17+
})
18+
);

packages/components/src/components/Example/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import React from 'react';
22
import styled from 'styled-components';
3+
import css from '@styled-system/css';
4+
import { Element } from '../Element';
35

4-
const H1 = styled.h1`
5-
color: red;
6-
`;
6+
const H1 = styled(Element).attrs({ as: 'h1' })(
7+
css({
8+
color: 'reds.500',
9+
fontSize: 7,
10+
})
11+
);
712

813
const ExampleComponent = () => <H1>I am an example component</H1>;
914

packages/components/src/components/Grid/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import styled from 'styled-components';
22
import css from '@styled-system/css';
3+
import { Element } from '../Element';
34

45
const fontSize = 1; // rem = 16px
56
const lineHeight = fontSize * 1.5;
67

7-
export const Grid = styled.div<{ columnGap?: number; rowGap?: number }>(
8+
export const Grid = styled(Element)<{ columnGap?: number; rowGap?: number }>(
89
({ columnGap, rowGap }) =>
910
css({
1011
display: 'grid',
@@ -19,7 +20,7 @@ export const Grid = styled.div<{ columnGap?: number; rowGap?: number }>(
1920
// valid combinations are
2021
// start | start + end | start + span | span
2122
// span + end is also possible but not implemented here
22-
export const Column = styled.div<{
23+
export const Column = styled(Element)<{
2324
start?: number;
2425
end?: number;
2526
span?: number;

packages/components/src/components/Stack/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import styled from 'styled-components';
22
import css from '@styled-system/css';
3+
import { Element } from '../Element';
34

4-
export const Stack = styled.div<{
5+
export const Stack = styled(Element)<{
56
gap?: number; // theme.space token
67
direction?: 'horizontal' | 'vertical';
78
justify?: string;

packages/components/src/components/Stack/stack.stories.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ export default {
1111

1212
// replace the text inside with Text variants when available
1313
export const Defaults = () => (
14-
<Stack style={{ height: 100, background: '#343434' }}>
14+
<Stack style={{ height: 100 }}>
1515
<div />
1616
<div />
1717
</Stack>
1818
);
1919

2020
export const WithGap = () => (
21-
<Stack gap={4} style={{ height: 100, background: '#343434' }}>
21+
<Stack gap={4} style={{ height: 100 }}>
2222
<div />
2323
<div>spacing token as gap</div>
2424
<div />
2525
</Stack>
2626
);
2727

2828
export const Justify = () => (
29-
<Stack justify="space-around" style={{ height: 100, background: '#343434' }}>
29+
<Stack justify="space-around" style={{ height: 100 }}>
3030
<div />
3131
<div />
3232
</Stack>
3333
);
3434

3535
export const Align = () => (
36-
<Stack align="center" style={{ height: 100, background: '#343434' }}>
36+
<Stack align="center" style={{ height: 100 }}>
3737
<div />
3838
<div />
3939
</Stack>

packages/components/src/stories/decorators/ThemeDecorator.tsx

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)