Skip to content

Commit eec5aef

Browse files
authored
fix: refactor Button and IconButton to children-based API (#1281)
* 🚧 Remove label and icon props * 🚧 Adding linkOptions * 🚧 Upgrade versioning * 🚧 Add icon button changes * 🚧 Fix linting * 🚧 Requested changes * 🚧 Fix tests * 🚧 Requested changes * 🚧 Requested changes * 🚧 Undo dev changes * 🚧 Improve typing * 🚧 Newly agreed changes * 🚧 Oops
1 parent 271ac21 commit eec5aef

22 files changed

Lines changed: 267 additions & 185 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@equinor/amplify-component-lib",
3-
"version": "13.0.0",
3+
"version": "13.1.0",
44
"description": "Frontend Typescript components for the Amplify team",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/molecules/Banner/Banner.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const Variants: Story = {
7272
export const CustomContent: Story = {
7373
tags: ['test-only'],
7474
args: {
75-
children: <Button label="Custom button"></Button>,
75+
children: <Button>Custom button</Button>,
7676
},
7777
play: async ({ canvas }) => {
7878
await expect(

src/molecules/Button/Button.stories.tsx

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { ChangeEvent, useState } from 'react';
22

3-
import { Checkbox, Snackbar, Tooltip } from '@equinor/eds-core-react';
3+
import { Checkbox, Icon, Snackbar, Tooltip } from '@equinor/eds-core-react';
44
import { chevron_down, save } from '@equinor/eds-icons';
55
import { Meta, StoryObj } from '@storybook/react-vite';
66

77
import { spacings } from 'src/atoms/style';
88
import { Button } from 'src/molecules/Button/Button';
99
import { Stack } from 'src/storybook';
10-
import { VariantShowcase } from 'src/storybook/VariantShowcase';
1110

1211
import { expect, fn, userEvent } from 'storybook/test';
1312

@@ -53,39 +52,54 @@ const meta: Meta<typeof Button> = {
5352
type: 'select',
5453
},
5554
},
55+
leadingContent: {
56+
table: { type: { summary: 'ReactNode' } },
57+
},
58+
trailingContent: {
59+
table: { type: { summary: 'ReactNode' } },
60+
},
5661
},
5762
};
5863

5964
export default meta;
6065
type Story = StoryObj<typeof Button>;
6166
export const Introduction: Story = {
62-
render: (args) => <Button {...args} label="You can control me" />,
67+
render: (args) => <Button {...args}>You can control me</Button>,
6368
};
6469

6570
export const Basic: Story = {
66-
render: (args) => (
67-
<VariantShowcase
68-
GenericComponent={Button}
69-
otherProps={{ ...args, label: 'Button' }}
70-
columns={[
71-
{ label: 'Filled', value: { variant: 'filled' } },
72-
{ label: 'Outlined', value: { variant: 'outlined' } },
73-
{ label: 'Ghost', value: { variant: 'ghost' } },
74-
]}
75-
rows={[
76-
{ label: 'Primary', value: { color: 'primary' } },
77-
{ label: 'Danger', value: { color: 'danger' } },
78-
]}
79-
/>
71+
render: () => (
72+
<div
73+
style={{ display: 'flex', flexDirection: 'column', gap: spacings.medium }}
74+
>
75+
<div style={{ display: 'flex', gap: spacings.medium }}>
76+
<Button>Filled</Button>
77+
<Button variant="outlined">Outlined</Button>
78+
<Button variant="ghost">Ghost</Button>
79+
</div>
80+
<div style={{ display: 'flex', gap: spacings.medium }}>
81+
<Button color="danger">Filled</Button>
82+
<Button color="danger" variant="outlined">
83+
Outlined
84+
</Button>
85+
<Button color="danger" variant="ghost">
86+
Ghost
87+
</Button>
88+
</div>
89+
</div>
8090
),
8191
};
8292

8393
export const Disabled: Story = {
8494
render: () => (
8595
<div style={{ display: 'flex', gap: spacings.medium }}>
86-
<Button label="Filled" disabled />
87-
<Button label="Outlined" disabled variant="outlined" />
88-
<Button label="Ghost" disabled variant="ghost" />
96+
<Button disabled>Filled</Button>
97+
<Button disabled variant="outlined">
98+
Outlined
99+
</Button>
100+
<Button disabled variant="ghost">
101+
Ghost
102+
</Button>
89103
</div>
90104
),
91105
};
@@ -104,12 +118,13 @@ const AccessibilityComponent = () => {
104118
/>
105119
<Tooltip title={canSubmit ? '' : 'Terms & Conditions must be checked'}>
106120
<Button
107-
label="Submit"
108121
aria-disabled={!canSubmit}
109122
onClick={() => {
110123
if (canSubmit) setOpen(true);
111124
}}
112-
/>
125+
>
126+
Submit
127+
</Button>
113128
</Tooltip>
114129
<Snackbar
115130
open={open}
@@ -130,13 +145,14 @@ export const Icons: Story = {
130145
render: () => (
131146
<>
132147
<div style={{ display: 'flex', gap: spacings.medium }}>
133-
<Button label="Leading icon" leadingIcon={save} />
134-
<Button label="Trailing icon" trailingIcon={save} />
148+
<Button leadingContent={<Icon data={save} />}>Leading icon</Button>
149+
<Button trailingContent={<Icon data={save} />}>Trailing icon</Button>
135150
<Button
136-
label="Both icons"
137-
leadingIcon={save}
138-
trailingIcon={chevron_down}
139-
/>
151+
leadingContent={<Icon data={save} />}
152+
trailingContent={<Icon data={chevron_down} />}
153+
>
154+
Both icons
155+
</Button>
140156
</div>
141157
</>
142158
),
@@ -152,21 +168,31 @@ export const Loading: Story = {
152168
}}
153169
>
154170
<div style={{ display: 'flex', gap: spacings.medium }}>
155-
<Button label="Filled" loading />
156-
<Button label="Outlined" loading variant="outlined" />
157-
<Button label="Ghost" loading variant="ghost" />
171+
<Button loading>Filled</Button>
172+
<Button loading variant="outlined">
173+
Outlined
174+
</Button>
175+
<Button loading variant="ghost">
176+
Ghost
177+
</Button>
158178
</div>
159179
<div style={{ display: 'flex', gap: spacings.medium }}>
160-
<Button label="Filled" loading color="danger" />
161-
<Button label="Outlined" loading color="danger" variant="outlined" />
162-
<Button label="Ghost" loading color="danger" variant="ghost" />
180+
<Button loading color="danger">
181+
Filled
182+
</Button>
183+
<Button loading color="danger" variant="outlined">
184+
Outlined
185+
</Button>
186+
<Button loading color="danger" variant="ghost">
187+
Ghost
188+
</Button>
163189
</div>
164190
</div>
165191
),
166192
};
167193

168194
export const FullWidth: Story = {
169-
render: () => (
195+
render: (args) => (
170196
<div
171197
style={{
172198
margin: '32px',
@@ -175,26 +201,34 @@ export const FullWidth: Story = {
175201
width: '100%',
176202
}}
177203
>
178-
<Button label="fullWidth" fullWidth leadingIcon={save} />
179-
<Button label="fullWidth" fullWidth trailingIcon={save} />
180-
<Button label="No fullWidth" leadingIcon={save} />
204+
<Button {...args} fullWidth leadingContent={<Icon data={save} />}>
205+
fullWidth
206+
</Button>
207+
<Button {...args} fullWidth trailingContent={<Icon data={save} />}>
208+
fullWidth
209+
</Button>
210+
<Button {...args} leadingContent={<Icon data={save} />}>
211+
No fullWidth
212+
</Button>
181213
</div>
182214
),
183215
name: 'Full width',
184216
};
185217

186218
export const LinkButton: Story = {
187219
render: () => {
188-
return <Button to="/faq" label="I am a link" />;
220+
return <Button linkOptions={{ to: '/faq' }}>I am a link</Button>;
189221
},
190222
name: 'Button as a link',
191223
};
192224

193225
export const TestLoadingState: Story = {
194226
tags: ['test-only'],
227+
render: (args) => {
228+
return <Button {...args}>Loading Button</Button>;
229+
},
195230
args: {
196231
loading: true,
197-
label: 'Loading Button',
198232
onClick: fn(),
199233
},
200234
play: async ({ canvas, args }) => {
@@ -208,7 +242,7 @@ export const TestLoadingState: Story = {
208242

209243
export const TestRendersAsLink: Story = {
210244
tags: ['test-only'],
211-
render: () => <Button to="/somewhere" label="Save" />,
245+
render: () => <Button linkOptions={{ to: '/somewhere' }}>Save</Button>,
212246
play: async ({ canvas }) => {
213247
const link = canvas.getByRole('link', { name: 'Save' });
214248

src/molecules/Button/Button.styles.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Icon } from '@equinor/eds-core-react';
21
import { tokens } from '@equinor/eds-tokens';
32
import { typographyTemplate } from '@equinor/eds-utils';
43

@@ -18,7 +17,10 @@ export const resolveBorderColor = (tokens: {
1817
}) => ('borderColor' in tokens ? tokens.borderColor : tokens.backgroundColor);
1918

2019
export const ButtonPrimitive = styled.button<ButtonStyles>`
21-
display: inline-flex;
20+
display: grid;
21+
grid-auto-flow: column;
22+
grid-template-columns: auto;
23+
2224
align-items: center;
2325
justify-content: center;
2426
height: ${shape.button.minHeight};
@@ -39,21 +41,33 @@ export const ButtonPrimitive = styled.button<ButtonStyles>`
3941
position: relative;
4042
cursor: pointer;
4143
42-
> span {
43-
padding: ${spacings.x_small};
44-
text-align: center;
45-
}
46-
4744
svg {
4845
justify-self: center;
49-
${(props) =>
50-
props.$fullWidth &&
51-
css`
52-
position: absolute;
53-
margin: 0 ${spacings.small};
54-
`}
5546
}
5647
48+
${(props) =>
49+
props.$fullWidth &&
50+
css`
51+
&:has(> :nth-child(2)) {
52+
grid-template-columns: 1fr auto 1fr;
53+
54+
> :first-child:not(.central-content) {
55+
grid-column: 1;
56+
justify-self: start;
57+
}
58+
59+
.central-content {
60+
grid-column: 2;
61+
justify-self: center;
62+
}
63+
64+
> :last-child:not(.central-content) {
65+
grid-column: 3;
66+
justify-self: end;
67+
}
68+
}
69+
`}
70+
5771
&::after {
5872
position: absolute;
5973
content: '';
@@ -93,25 +107,20 @@ export const ButtonPrimitive = styled.button<ButtonStyles>`
93107
}
94108
`;
95109

110+
export const PaddedContent = styled.span`
111+
padding: ${spacings.x_small};
112+
`;
113+
96114
export const HiddenContent = styled.span`
97115
opacity: 0;
98116
visibility: hidden;
99117
`;
100118

101119
export const CenteredContent = styled.span`
102-
left: 0;
103120
position: absolute;
104121
width: 100%;
105122
height: 100%;
106123
display: flex;
107124
align-items: center;
108125
justify-content: center;
109126
`;
110-
111-
export const LeftIcon = styled(Icon)`
112-
left: 0;
113-
`;
114-
115-
export const RightIcon = styled(Icon)`
116-
right: 0;
117-
`;

0 commit comments

Comments
 (0)