Skip to content

Commit 6d0a5e9

Browse files
authored
Use classcat to clean up logic around className props (#46)
1 parent 6f20837 commit 6d0a5e9

File tree

8 files changed

+35
-14
lines changed

8 files changed

+35
-14
lines changed

components/button.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import React, { MouseEventHandler } from 'react';
33
import { useTheme } from '../hooks/theme/index';
44
import { ReactChildrenProps } from '../types';
5+
import cc from 'classcat';
56

67
type ButtonProps = {
78
onClick?: MouseEventHandler;
@@ -48,7 +49,7 @@ export default function Button({
4849
{...buttonAttrs}
4950
type={buttonType || 'button'}
5051
onClick={onClick}
51-
className={`${className} ${sizeClassName}`}
52+
className={cc([className, sizeClassName])}
5253
>
5354
{children}
5455
</button>

components/link.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import React, { FunctionComponent } from 'react';
33
import { default as NextLink } from 'next/link';
44
import { useTheme } from '../hooks/theme/index';
5+
import cc from 'classcat';
56

67
const underlineClassNames = {
78
sm: 'underline-offset-sm',
@@ -43,11 +44,9 @@ const Link: FunctionComponent<LinkProps> = ({
4344
classNames.push(underlineOffsetClassName);
4445
}
4546

46-
const className = classNames.join(' ');
47-
4847
return (
4948
<NextLink passHref={true} href={href}>
50-
<a {...attrs} className={className}>
49+
<a {...attrs} className={cc(classNames)}>
5150
{children}
5251
</a>
5352
</NextLink>

components/modal.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { KeyboardEventHandler } from 'react';
22
import FocusLock from 'react-focus-lock';
33
import { ReactChildrenProps } from '../types';
4+
import cc from 'classcat';
45

56
type ModalProps = {
67
onClose: () => void;
@@ -37,7 +38,7 @@ export default function Modal({
3738
<div
3839
role="dialog"
3940
aria-modal="true"
40-
className={classNames.join(' ')}
41+
className={cc(classNames)}
4142
onKeyDown={onKeyDown}
4243
>
4344
{children}

components/rhythm-edit/rhythm-edit.tsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { denominatorTerm } from '../../utils/denominator-term';
44
import { numeratorTerm } from '../../utils/numerator-term';
55
import { ValidationWrapper } from './validated-wrapper';
66
import { Rhythm, UnsavedRhythm } from '../../types';
7+
import cc from 'classcat';
78

89
const sharedSelectAndInputClassNames = [
910
'text-gray-800',
@@ -19,13 +20,13 @@ const inputClassNames = [
1920
'py-1',
2021
'px-2',
2122
'placeholder-gray-300',
22-
].join(' ');
23+
];
2324

2425
const selectClassNames = [
2526
...sharedSelectAndInputClassNames,
2627
'p-1',
2728
'w-48',
28-
].join(' ');
29+
];
2930

3031
const generateNumeratorSelect = (
3132
selected: string,
@@ -41,7 +42,7 @@ const generateNumeratorSelect = (
4142

4243
return (
4344
<select
44-
className={selectClassNames}
45+
className={cc(selectClassNames)}
4546
onChange={onChange}
4647
aria-label="Rhythm action count"
4748
value={selected}
@@ -65,7 +66,7 @@ const generateDenominatorSelect = (
6566

6667
return (
6768
<select
68-
className={selectClassNames}
69+
className={cc(selectClassNames)}
6970
onChange={onChange}
7071
aria-label="Rhythm action count time span"
7172
value={selected}
@@ -188,7 +189,7 @@ export default function RhythmEdit({
188189
error={validationResult.errors.action}
189190
>
190191
<input
191-
className={inputClassNames}
192+
className={cc(inputClassNames)}
192193
aria-label="Rhythm action description"
193194
aria-required="true"
194195
placeholder="get into a rhythm"
@@ -215,7 +216,7 @@ export default function RhythmEdit({
215216
error={validationResult.errors.reason}
216217
>
217218
<input
218-
className={inputClassNames}
219+
className={cc(inputClassNames)}
219220
aria-label="Rhythm reason description"
220221
aria-required="true"
221222
placeholder="it will bring positive change"

components/rhythm/occurrence.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { format } from 'date-fns';
33
import chroma from 'chroma-js';
44
import { useTheme } from '../../hooks/theme/index';
55
import { pullBackgroundColor } from '../../utils/pull-background-color';
6+
import cc from 'classcat';
67

78
function createCooldownStyle(
89
cooldown: number,
@@ -67,7 +68,7 @@ export default function Occurrence({
6768
onClick={onClick}
6869
className="flex p-4 -m-4"
6970
>
70-
<div className={classNames.join(' ')}></div>
71+
<div className={cc(classNames)}></div>
7172
</button>
7273
);
7374
} else {
@@ -86,7 +87,7 @@ export default function Occurrence({
8687
role="img"
8788
aria-label={ariaLabel}
8889
style={style}
89-
className={classNames.join(' ')}
90+
className={cc(classNames)}
9091
></div>
9192
);
9293
}

components/stripe.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import React from 'react';
22
import { useTheme } from '../hooks/theme/index';
3+
import cc from 'classcat';
34

45
export default function Stripe() {
56
const [theme] = useTheme();
67
const { stripeBgClass } = theme;
78

9+
const className = cc([
10+
stripeBgClass,
11+
'absolute',
12+
'top-0',
13+
'right-0',
14+
'w-8',
15+
'md:w-12',
16+
'h-full',
17+
'z-20',
18+
]);
19+
820
return (
921
<div
10-
className={`${stripeBgClass} absolute top-0 right-0 w-8 md:w-12 h-full z-20`}
22+
className={className}
1123
></div>
1224
);
1325
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"dependencies": {
1616
"chroma-js": "^2.4.2",
17+
"classcat": "^5.0.4",
1718
"date-fns": "^2.28.0",
1819
"immer": "^9.0.12",
1920
"mockdate": "^3.0.5",

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -5042,6 +5042,11 @@ class-utils@^0.3.5:
50425042
isobject "^3.0.0"
50435043
static-extend "^0.1.1"
50445044

5045+
classcat@^5.0.4:
5046+
version "5.0.4"
5047+
resolved "https://registry.yarnpkg.com/classcat/-/classcat-5.0.4.tgz#e12d1dfe6df6427f260f03b80dc63571a5107ba6"
5048+
integrity sha512-sbpkOw6z413p+HDGcBENe498WM9woqWHiJxCq7nvmxe9WmrUmqfAcxpIwAiMtM5Q3AhYkzXcNQHqsWq0mND51g==
5049+
50455050
classnames@^2.2.5:
50465051
version "2.3.1"
50475052
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.3.1.tgz#dfcfa3891e306ec1dad105d0e88f4417b8535e8e"

0 commit comments

Comments
 (0)