Skip to content

Commit 5d18d4d

Browse files
committed
[ui components] Refactor ButtonLink to use CSS module
1 parent 9600020 commit 5d18d4d

File tree

3 files changed

+76
-58
lines changed

3 files changed

+76
-58
lines changed
Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import styled, {css} from 'styled-components';
1+
import clsx from 'clsx';
2+
import {CSSProperties} from 'react';
23

34
import {Colors} from './Color';
5+
import styles from './css/ButtonLink.module.css';
46

57
type Colors =
68
| string
@@ -12,67 +14,46 @@ type Colors =
1214

1315
type Underline = 'never' | 'always' | 'hover';
1416

15-
const fontColor = (color: Colors) => {
16-
if (typeof color === 'string') {
17-
return css`
18-
color: ${color};
19-
`;
20-
}
21-
22-
const {link, hover, active} = color;
23-
return css`
24-
color: ${link};
25-
${hover ? `&:hover { color: ${hover}; }` : null}
26-
${active ? `&:active { color: ${active}; }` : null}
27-
`;
17+
const getColorVars = (color: Colors) => {
18+
const values = typeof color === 'string' ? {link: color, hover: color, active: color} : color;
19+
return {
20+
'--button-link-color': values.link,
21+
'--button-link-hover-color': values.hover,
22+
'--button-link-active-color': values.active,
23+
} as CSSProperties;
2824
};
2925

30-
const textDecoration = (underline: Underline) => {
31-
switch (underline) {
32-
case 'always':
33-
return css`
34-
text-decoration: underline;
35-
`;
36-
case 'hover':
37-
return css`
38-
&:hover:not(:disabled) {
39-
text-decoration: underline;
40-
}
41-
`;
42-
case 'never':
43-
default:
44-
return null;
45-
}
26+
const getTextDecorationClassName = (underline: Underline) => {
27+
return clsx(
28+
underline === 'always' && styles.textDecorationAlways,
29+
underline === 'hover' && styles.textDecorationHover,
30+
);
4631
};
4732

4833
interface Props extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'color'> {
4934
color?: Colors;
5035
underline?: Underline;
5136
}
5237

53-
export const ButtonLink = styled(({color: _color, underline: _underline, ...rest}: Props) => (
54-
<button {...rest} />
55-
))`
56-
background: transparent;
57-
border: 0;
58-
cursor: pointer;
59-
font-size: inherit;
60-
line-height: 1;
61-
padding: 8px;
62-
margin: -8px;
63-
text-align: left;
64-
65-
&:disabled {
66-
cursor: default;
67-
opacity: 0.7;
68-
}
69-
70-
&:focus:not(:focus-visible) {
71-
outline: none;
72-
}
73-
74-
transition: 30ms color linear;
75-
76-
${({color}) => fontColor(color || Colors.linkDefault())}
77-
${({underline}) => textDecoration(underline || 'hover')}
78-
`;
38+
export const ButtonLink = ({
39+
color,
40+
underline,
41+
style: userStyle,
42+
className: userClassName,
43+
...rest
44+
}: Props) => {
45+
const colors = color ? getColorVars(color) : {};
46+
const textDecoration = underline ? getTextDecorationClassName(underline) : undefined;
47+
const style = {
48+
...userStyle,
49+
...colors,
50+
};
51+
52+
return (
53+
<button
54+
{...rest}
55+
style={style}
56+
className={clsx(styles.buttonLink, textDecoration, userClassName)}
57+
/>
58+
);
59+
};

js_modules/dagster-ui/packages/ui-components/src/components/__stories__/ButtonLink.stories.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {StoryFn} from '@storybook/nextjs';
2-
import * as React from 'react';
32

43
import {ButtonLink} from '../ButtonLink';
54
import {Colors} from '../Color';
@@ -25,7 +24,7 @@ ColorMap.args = {
2524
color: {
2625
link: Colors.linkDefault(),
2726
hover: Colors.linkHover(),
28-
active: Colors.linkHover(),
27+
active: Colors.accentGreen(),
2928
},
3029
};
3130

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.buttonLink {
2+
--button-link-color: var(--color-link-default);
3+
--button-link-hover-color: var(--color-link-default);
4+
--button-link-active-color: var(--color-link-default);
5+
6+
background: transparent;
7+
border: 0;
8+
color: var(--button-link-color);
9+
cursor: pointer;
10+
font-size: inherit;
11+
line-height: 1;
12+
padding: 8px;
13+
margin: -8px;
14+
text-align: left;
15+
transition: 30ms color linear;
16+
}
17+
18+
.buttonLink:disabled {
19+
cursor: default;
20+
opacity: 0.7;
21+
}
22+
23+
.buttonLink:hover {
24+
color: var(--button-link-hover-color);
25+
}
26+
27+
.buttonLink:active {
28+
color: var(--button-link-active-color);
29+
}
30+
31+
.buttonLink:focus:not(:focus-visible) {
32+
outline: none;
33+
}
34+
35+
.textDecorationAlways,
36+
.textDecorationHover:hover:not(:disabled) {
37+
text-decoration: underline;
38+
}

0 commit comments

Comments
 (0)