Skip to content

Commit e8452a5

Browse files
components: Switch (codesandbox#3296)
* add switch component * bonus: fix grid column types * weird but temporary * hard code temporary values * fix ts * make console an addon * add id to switch element Co-authored-by: Sara Vieira <[email protected]>
1 parent 89bfb5d commit e8452a5

File tree

5 files changed

+106
-3
lines changed

5 files changed

+106
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,5 @@ Thanks goes to these wonderful people
252252

253253
<!-- markdownlint-enable -->
254254
<!-- prettier-ignore-end -->
255+
255256
<!-- ALL-CONTRIBUTORS-LIST:END -->

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export const Grid = styled(Element)<{ columnGap?: number; rowGap?: number }>(
2121
// start | start + end | start + span | span
2222
// span + end is also possible but not implemented here
2323
export const Column = styled(Element)<{
24-
start?: number;
25-
end?: number;
26-
span?: number;
24+
start?: number | Array<number>;
25+
end?: number | Array<number>;
26+
span?: number | Array<number>;
2727
}>(({ start, end, span }) => {
2828
const styles: {
2929
gridColumnStart?: number | Array<number | string>;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React from 'react';
2+
import styled from 'styled-components';
3+
import css from '@styled-system/css';
4+
import { Element } from '../Element';
5+
6+
const SwitchBackground = styled.div(
7+
css({
8+
width: 7,
9+
height: 4,
10+
backgroundColor: 'switch.background',
11+
borderRadius: 'large',
12+
position: 'relative',
13+
})
14+
);
15+
16+
const SwitchToggle = styled.span(
17+
css({
18+
width: 4,
19+
height: 4,
20+
backgroundColor: 'switch.foregroundOff',
21+
borderRadius: '50%',
22+
position: 'absolute',
23+
left: 0,
24+
transition: 'left ease',
25+
transitionDuration: theme => theme.speeds[3],
26+
})
27+
);
28+
29+
const SwitchInput = styled.input(
30+
css({
31+
width: 0,
32+
opacity: 0,
33+
position: 'absolute',
34+
})
35+
);
36+
37+
const SwitchContainer = styled(Element)(
38+
css({
39+
'input:checked + [data-component=SwitchBackground]': {
40+
backgroundColor: 'switch.foregroundOn',
41+
},
42+
'input:checked + [data-component=SwitchBackground] [data-component=SwitchToggle]': {
43+
left: theme => theme.space[4] - 3,
44+
},
45+
})
46+
);
47+
48+
interface ISwitchProps {
49+
id: string;
50+
on?: boolean;
51+
defaultOn?: boolean;
52+
onChange?: (event: any) => void;
53+
}
54+
55+
export const Switch: React.FC<ISwitchProps> = ({
56+
id,
57+
on,
58+
defaultOn,
59+
...props
60+
}) => {
61+
if (!id) {
62+
console.warn('Please pass a id corresponding to the htmlFor of its label');
63+
}
64+
65+
return (
66+
<SwitchContainer as="label">
67+
<SwitchInput
68+
type="checkbox"
69+
id={id}
70+
checked={on}
71+
defaultChecked={defaultOn}
72+
{...props}
73+
/>
74+
<SwitchBackground data-component="SwitchBackground">
75+
<SwitchToggle data-component="SwitchToggle" />
76+
</SwitchBackground>
77+
</SwitchContainer>
78+
);
79+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import { action } from '@storybook/addon-actions';
3+
import { Switch } from '.';
4+
5+
export default {
6+
title: 'components/Switch',
7+
component: Switch,
8+
};
9+
10+
export const Basic = () => <Switch />;
11+
12+
export const defaultOn = () => <Switch defaultOn />;
13+
14+
/* eslint-disable no-console */
15+
export const onChange = () => <Switch onChange={action('changed')} />;

packages/components/src/utils/polyfill-theme.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ const polyfillTheme = vsCodeTheme =>
4747
// @ts-ignore: The colors totally exist, our typings are incorrect
4848
hoverBackground: `linear-gradient(0deg, rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0.2)), ${designLanguage.colors.reds[300]}`,
4949
},
50+
switch: {
51+
// @ts-ignore
52+
background: designLanguage.colors.grays[800],
53+
// @ts-ignore
54+
foregroundOff: designLanguage.colors.white,
55+
// @ts-ignore
56+
foregroundOn: designLanguage.colors.green,
57+
},
5058
});
5159

5260
export default polyfillTheme;

0 commit comments

Comments
 (0)