Skip to content

Commit fad5184

Browse files
authored
clean up tw config. update snapshots. replace Button css module with tw (#1900)
* clean up tw config. update snapshots. replace Button css module with tw * remove nulls from cva utils * clean up Button props and update snapshots * upgrade to cva@beta * fix cva args
1 parent bf8fe26 commit fad5184

39 files changed

Lines changed: 145 additions & 195 deletions

File tree

common/utils/cva.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-restricted-imports */
2-
import { cx as clsx, cva as cvaOriginal } from 'class-variance-authority';
2+
import { cx as clsx, cva as cvaOriginal } from 'cva';
33
import { twMerge } from 'tailwind-merge';
4-
import type { ClassValue } from 'class-variance-authority/dist/types';
4+
import type { ClassValue, CVA } from 'cva';
55
/* eslint-enable @typescript-eslint/no-restricted-imports */
66

77
/**
@@ -28,12 +28,12 @@ export const cx = (...classes: ClassValue[]) => twMerge(clsx(...classes));
2828
*
2929
* @see https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Handling_conflicts
3030
*/
31-
export const cva: typeof cvaOriginal = (base, config) => {
32-
const cvaFn = cvaOriginal(base, config);
31+
export const cva: CVA = props => {
32+
const cvaFn = cvaOriginal(props);
3333
return (...args: Parameters<typeof cvaFn>) => {
3434
const result = cvaFn(...args);
3535
return twMerge(result);
3636
};
3737
};
3838

39-
export type { VariantProps } from 'class-variance-authority';
39+
export type { VariantProps } from 'cva';

components/Branding/FontSection/FontSection.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function FontSection() {
2121
<div
2222
className={
2323
fontsObject[item] === 'DIN Condensed Bold'
24-
? '[&>p]:font-serif [&>h6]:font-serif [&>p]:font-dinCondensed [&>h6]:font-dinCondensed'
25-
: '[&>p]:font-sans [&>p]:font-encodeSans [&>h6]:font-sans [&>h6]:font-encodeSans'
24+
? '[&>p]:font-serif [&>h6]:font-serif [&>p]:font-din-condensed [&>h6]:font-din-condensed'
25+
: '[&>p]:font-sans [&>p]:font-encode-sans [&>h6]:font-sans [&>h6]:font-encode-sans'
2626
}
2727
>
2828
<h6 style={fontStyle}>{fontsObject[item]}</h6>

components/Branding/FontSection/__tests__/__snapshots__/FontSection.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`FontSection > should render with required props 1`] = `
99
>
1010
<li>
1111
<div
12-
className="[&>p]:font-serif [&>h6]:font-serif [&>p]:font-dinCondensed [&>h6]:font-dinCondensed"
12+
className="[&>p]:font-serif [&>h6]:font-serif [&>p]:font-din-condensed [&>h6]:font-din-condensed"
1313
>
1414
<h6
1515
style={
@@ -33,7 +33,7 @@ exports[`FontSection > should render with required props 1`] = `
3333
</li>
3434
<li>
3535
<div
36-
className="[&>p]:font-sans [&>p]:font-encodeSans [&>h6]:font-sans [&>h6]:font-encodeSans"
36+
className="[&>p]:font-sans [&>p]:font-encode-sans [&>h6]:font-sans [&>h6]:font-encode-sans"
3737
>
3838
<h6
3939
style={

components/Buttons/Button/Button.module.css

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

components/Buttons/Button/Button.tsx

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { ButtonHTMLAttributes } from 'react';
2+
import type { VariantProps } from 'common/utils/cva';
23
import noop from 'lodash/noop';
3-
import { cx } from 'common/utils/cva';
4+
import { cva } from 'common/utils/cva';
45
import { BUTTON } from 'common/constants/testIDs';
56
import { gtag } from 'common/utils/thirdParty/gtag';
67
import { getDataAttributes, getAriaAttributes } from 'common/utils/prop-utils';
7-
import styles from './Button.module.css';
88

99
interface GoogleAnalyticsEventPropType {
1010
/**
@@ -36,20 +36,41 @@ interface GoogleAnalyticsEventPropType {
3636
transport?: 'beacon' | 'xhr' | 'image';
3737
}
3838

39-
type ButtonProps = {
39+
interface ButtonProps
40+
extends ButtonHTMLAttributes<HTMLButtonElement>,
41+
VariantProps<typeof buttonCva> {
4042
/**
4143
* Helps track in-page `event` interactions.
4244
*/
4345
analyticsObject?: GoogleAnalyticsEventPropType;
44-
/**
45-
* Forces the component's width as wide as its parent container's width.
46-
*/
47-
fullWidth?: boolean;
48-
/**
49-
* Sets the button color theme.
50-
*/
51-
theme?: 'primary' | 'secondary';
52-
} & ButtonHTMLAttributes<HTMLButtonElement>;
46+
}
47+
48+
export const buttonCva = cva({
49+
base: [
50+
'inline-block font-din-condensed rounded leading-none border-4 border-solid cursor-pointer font-bold text-center uppercase py-4 px-3 whitespace-nowrap transition-all duration-200 ease-linear min-w-[175px]',
51+
'focus-visible:bg-transparent hover:bg-transparent',
52+
'disabled:opacity-60 disabled:hover:cursor-not-allowed',
53+
],
54+
variants: {
55+
theme: {
56+
primary: 'bg-primary border-primary outline-primary text-secondary',
57+
secondary: 'bg-secondary border-secondary outline-secondary text-primary',
58+
},
59+
},
60+
compoundVariants: [
61+
{
62+
theme: 'primary',
63+
class: 'focus-visible:text-primary hover:text-primary',
64+
},
65+
{
66+
theme: 'secondary',
67+
class: 'focus-visible:text-secondary hover:text-secondary',
68+
},
69+
],
70+
defaultVariants: {
71+
theme: 'primary',
72+
},
73+
});
5374

5475
export default function Button({
5576
analyticsObject = {
@@ -59,27 +80,22 @@ export default function Button({
5980
children,
6081
className = undefined,
6182
disabled = false,
62-
fullWidth = false,
6383
onClick = noop,
6484
tabIndex = 0,
65-
theme = 'primary',
85+
theme,
6686
type = 'button',
6787
...rest
6888
}: ButtonProps) {
6989
const customDataAttributes = getDataAttributes(rest);
7090
const ariaAttributes = getAriaAttributes(rest);
71-
7291
const eventConfig = {
7392
...analyticsObject,
7493
label: typeof children === 'string' ? children : undefined,
7594
};
7695

7796
return (
7897
<button
79-
className={cx(styles.Button, className, styles[theme], {
80-
[styles.disabled]: disabled,
81-
[styles.fullWidth]: fullWidth,
82-
})}
98+
className={buttonCva({ theme, className })}
8399
data-testid={BUTTON}
84100
disabled={disabled}
85101
onClick={e => {

components/Buttons/Button/__stories__/Button.stories.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ type ButtonStoryType = StoryObj<typeof Button>;
66
const meta: Meta<typeof Button> = {
77
title: 'Buttons/Button',
88
component: Button,
9+
args: {
10+
theme: 'primary',
11+
disabled: false,
12+
},
13+
argTypes: {
14+
disabled: { control: 'boolean' },
15+
},
916
parameters: {
1017
actions: {
1118
handles: ['click'],

components/Buttons/Button/__tests__/__snapshots__/Button.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`Button > should render with many props assigned 1`] = `
44
<button
5-
className="Button test-class secondary disabled fullWidth"
5+
className="inline-block font-din-condensed rounded leading-none border-4 border-solid cursor-pointer font-bold text-center uppercase py-4 px-3 whitespace-nowrap transition-all duration-200 ease-linear min-w-[175px] focus-visible:bg-transparent hover:bg-transparent disabled:opacity-60 disabled:hover:cursor-not-allowed bg-secondary border-secondary outline-secondary text-primary focus-visible:text-secondary hover:text-secondary test-class"
66
data-id="test-id"
77
data-testid="BUTTON"
88
disabled={true}
@@ -16,7 +16,7 @@ exports[`Button > should render with many props assigned 1`] = `
1616

1717
exports[`Button > should render with required props 1`] = `
1818
<button
19-
className="Button primary"
19+
className="inline-block font-din-condensed rounded leading-none border-4 border-solid cursor-pointer font-bold text-center uppercase py-4 px-3 whitespace-nowrap transition-all duration-200 ease-linear min-w-[175px] focus-visible:bg-transparent hover:bg-transparent disabled:opacity-60 disabled:hover:cursor-not-allowed bg-primary border-primary outline-primary text-secondary focus-visible:text-primary hover:text-primary"
2020
data-testid="BUTTON"
2121
disabled={false}
2222
onClick={[Function]}

components/Buttons/LinkButton/LinkButton.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { cx } from 'common/utils/cva';
21
import Link from 'next/link';
32
import OutboundLink from 'components/OutboundLink/OutboundLink';
4-
import styles from '../Button/Button.module.css';
3+
import type { VariantProps } from 'common/utils/cva';
4+
import { buttonCva } from '../Button/Button';
55

6-
export interface LinkButtonProps {
6+
export interface LinkButtonProps extends VariantProps<typeof buttonCva> {
77
/**
88
* Url to be passed to the base anchor element.
99
*/
@@ -24,28 +24,17 @@ export interface LinkButtonProps {
2424
* Sets an id to the base element for testing.
2525
*/
2626
'data-testid'?: string;
27-
/**
28-
* Forces the component's width as wide as its parent container's width.
29-
*/
30-
fullWidth?: boolean;
31-
/**
32-
* Sets the button color theme.
33-
*/
34-
theme?: 'primary' | 'secondary';
3527
}
3628

3729
export default function LinkButton({
3830
analyticsEventLabel,
3931
children,
4032
className,
4133
'data-testid': testID,
42-
fullWidth = false,
4334
href,
44-
theme = 'primary',
35+
theme,
4536
}: LinkButtonProps) {
46-
const linkButtonClassNames = cx(styles.Button, className, styles[theme], {
47-
[styles.fullWidth]: fullWidth,
48-
});
37+
const linkButtonClassNames = buttonCva({ theme, className });
4938

5039
const hasAnalyticsEventLabel = !!analyticsEventLabel;
5140

components/Buttons/LinkButton/__stories__/LinkButton.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ type LinkButtonStoryType = StoryObj<typeof LinkButton>;
66
const meta: Meta<typeof LinkButton> = {
77
title: 'Buttons/LinkButton',
88
component: LinkButton,
9+
args: {
10+
theme: 'primary',
11+
},
912
};
1013

1114
export default meta;

components/Buttons/LinkButton/__tests__/__snapshots__/LinkButton.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`LinkButton > should render with many props assigned 1`] = `
44
<a
5-
className="inline-flex items-start Button test-class secondary fullWidth"
5+
className="items-start inline-block font-din-condensed rounded leading-none border-4 border-solid cursor-pointer font-bold text-center uppercase py-4 px-3 whitespace-nowrap transition-all duration-200 ease-linear min-w-[175px] focus-visible:bg-transparent hover:bg-transparent disabled:opacity-60 disabled:hover:cursor-not-allowed bg-secondary border-secondary outline-secondary text-primary focus-visible:text-secondary hover:text-secondary test-class"
66
data-testid="Test"
77
href="https://tests.com"
88
onClick={[Function]}
@@ -21,7 +21,7 @@ exports[`LinkButton > should render with many props assigned 1`] = `
2121

2222
exports[`LinkButton > should render with required props 1`] = `
2323
<a
24-
className="Button primary"
24+
className="inline-block font-din-condensed rounded leading-none border-4 border-solid cursor-pointer font-bold text-center uppercase py-4 px-3 whitespace-nowrap transition-all duration-200 ease-linear min-w-[175px] focus-visible:bg-transparent hover:bg-transparent disabled:opacity-60 disabled:hover:cursor-not-allowed bg-primary border-primary outline-primary text-secondary focus-visible:text-primary hover:text-primary"
2525
href="https://tests.com"
2626
onClick={[Function]}
2727
onMouseEnter={[Function]}

0 commit comments

Comments
 (0)