Skip to content

Commit 91e77c3

Browse files
author
Michael Marszalek
authored
Rewrite of react button to engulf new button tokens (#43)
* Start rewrite of react button to engulf tokens * fixed after,before position * ignore .env's * Added focus with outline for now * Added disabled styling * Updates to alignment and offset calculation * Added unit type to token values
1 parent 1b43baf commit 91e77c3

File tree

14 files changed

+9065
-161
lines changed

14 files changed

+9065
-161
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dist
22
.cache
33
node_modules
44
pnpm-debug.log
5+
.env

apps/figma-broker/files/desktop-ui/buttons.js

+36-18
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ import * as R from 'ramda'
99

1010
const fallback = {}
1111

12+
const rootFontSize = 16
13+
14+
const px = (unit) => `${unit}px`
15+
const em = (unit) => `${unit}em`
16+
const rem = (unit) => `${unit}rem`
17+
1218
const buildProps = (states) => {
1319
let buttonProps = {}
1420

1521
const pressed = R.find(withName('pressed'), states)
1622
const hovered = R.find(withName('hover'), states)
1723
const focused = R.find(withName('focused'), states)
1824
const enabled = R.find(withName('enabled'), states)
25+
const disabled = R.find(withName('disabled'), states)
1926

20-
if (enabled) {
21-
const components = enabled.children
27+
if (enabled || disabled) {
28+
const components = (enabled || disabled).children
2229
const button = R.find(withName('button'), components)
2330
const label = R.find(withName('label'), components)
2431
const spacing = R.filter(withName('spacing'), components)
@@ -27,44 +34,53 @@ const buildProps = (states) => {
2734
if (button) {
2835
const button_ = R.head(button.children)
2936
const { cornerRadius, strokeWeight, absoluteBoundingBox } = button_
30-
const {height} = absoluteBoundingBox
37+
const { height } = absoluteBoundingBox
3138
const fill = button_.fills.find(withType('solid')) || fallback
3239
const stroke = button_.strokes.find(withType('solid')) || fallback
3340

3441
buttonProps = {
3542
...buttonProps,
36-
cornerRadius,
37-
height,
43+
height: px(height),
3844
background: colortoRgba(fill.color),
39-
borderColor: colortoRgba(stroke.color),
40-
borderWidth: strokeWeight,
45+
border: {
46+
color: colortoRgba(stroke.color),
47+
width: px(strokeWeight),
48+
radius: px(cornerRadius),
49+
},
4150
}
4251
}
4352

4453
if (label) {
4554
const {
46-
fontPostScriptName,
55+
fontFamily,
4756
fontSize,
4857
fontWeight,
4958
letterSpacing,
50-
lineHeightPx,
59+
textAlignHorizontal = 'center',
60+
lineHeightPercentFontSize,
5161
} = label.style
62+
const fill = label.fills.find(withType('solid')) || fallback
5263

64+
const fontSizeRem = (fontSize / rootFontSize).toFixed(3)
65+
const lineHeightEm = (lineHeightPercentFontSize / 100).toFixed(3)
66+
const letterSpacingEm = (letterSpacing / fontSizeRem).toFixed(3)
5367
buttonProps = {
5468
...buttonProps,
69+
color: colortoRgba(fill.color),
5570
typography: {
56-
font: fontPostScriptName,
57-
fontSize,
71+
fontFamily,
72+
fontSize: rem(fontSizeRem),
5873
fontWeight,
59-
letterSpacing,
60-
lineHeight: lineHeightPx,
74+
letterSpacing: letterSpacing ? em(letterSpacingEm) : 0,
75+
lineHeight: em(lineHeightEm),
76+
textAlign: R.toLower(textAlignHorizontal),
6177
},
6278
}
6379
}
6480

6581
if (spacing.length > 0) {
6682
// Spacing can be used in any form, so we create an object
67-
// with names prefixed with "Spacing" in figma
83+
// with names prefixed with "Spacing" in figma´
6884
const spacingProps = R.reduce(
6985
(acc, val) => {
7086
const spacer = R.head(val.children)
@@ -76,7 +92,7 @@ const buildProps = (states) => {
7692
)
7793
return {
7894
...acc,
79-
[name]: spacingValue,
95+
[name]: px(spacingValue),
8096
}
8197
}
8298
return acc
@@ -94,10 +110,12 @@ const buildProps = (states) => {
94110
if (clickbounds) {
95111
const clickbound = R.head(clickbounds.children)
96112
const { height } = clickbound.absoluteBoundingBox
113+
const clickboundOffset = (height - parseInt(buttonProps.height, 10)) / 2
97114

98115
buttonProps = {
99116
...buttonProps,
100-
clickbound: height,
117+
clickbound: px(height),
118+
clickboundOffset: px(clickboundOffset),
101119
}
102120
}
103121
}
@@ -117,8 +135,8 @@ const buildProps = (states) => {
117135
focus: {
118136
type: focusStyle,
119137
color: colortoRgba(stroke.color),
120-
width: dashWidth,
121-
gap: dashGap,
138+
width: px(dashWidth),
139+
gap: px(dashGap),
122140
},
123141
}
124142
}

apps/figma-broker/files/desktop-ui/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ export const makeDesktopComponents = (figmaPages) => {
1414
name: 'buttons-primary',
1515
value: makeButtonsComponent(data),
1616
})
17+
break;
18+
case 'buttons secondary color':
19+
components.push({
20+
name: 'buttons-secondary',
21+
value: makeButtonsComponent(data),
22+
})
23+
break;
24+
case 'buttons danger color':
25+
components.push({
26+
name: 'buttons-danger',
27+
value: makeButtonsComponent(data),
28+
})
29+
break;
30+
case 'buttons disabled':
31+
components.push({
32+
name: 'buttons-disabled',
33+
value: makeButtonsComponent(data),
34+
})
35+
break;
1736
default:
1837
break
1938
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
import React from 'react'
22
import { storiesOf } from '@storybook/react'
33
import { Button } from '@eds-ui/core-react'
4+
import './button.css'
45

5-
storiesOf('Button', module).add('with text', () => <Button>Button</Button>)
6+
storiesOf('Button', module).add('Demo', () => (
7+
<div className="btn-container">
8+
<h1>Variations</h1>
9+
10+
<div className="btn-group">
11+
<h2>Contained (default)</h2>
12+
<Button>Primary</Button>
13+
<Button color="secondary">Secondary</Button>
14+
<Button color="danger">Danger</Button>
15+
<Button disabled>Disabled</Button>
16+
</div>
17+
18+
<div className="btn-group">
19+
<h2>Outlined</h2>
20+
<Button variant="outlined">Primary</Button>
21+
<Button variant="outlined" color="secondary">
22+
Secondary
23+
</Button>
24+
<Button variant="outlined" color="danger">
25+
Danger
26+
</Button>
27+
<Button variant="outlined" disabled>
28+
Disabled
29+
</Button>
30+
</div>
31+
32+
<div className="btn-group">
33+
<h2>Ghost</h2>
34+
<Button variant="ghost">Primary</Button>
35+
<Button variant="ghost" color="secondary">
36+
Secondary
37+
</Button>
38+
<Button variant="ghost" color="danger">
39+
Danger
40+
</Button>
41+
<Button variant="ghost" disabled>
42+
Disabled
43+
</Button>
44+
</div>
45+
</div>
46+
))
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.btn-container {
2+
margin: 32px;
3+
}
4+
5+
.btn-group {
6+
margin-top: 48px;
7+
}
8+
.btn-group > button {
9+
margin-right: 32px;
10+
}

0 commit comments

Comments
 (0)