|
1
|
export const metadata = { |
|
2
|
title: `clx`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
`clx` is a utility function that adds class names to your components, with support for conditional classes and merging Tailwind CSS classes. |
|
8
|
|
|
9
|
In this guide, you'll learn how to use the `clx` utility function. |
|
10
|
|
|
11
|
## Usage |
|
12
|
|
|
13
|
|
|
14
|
The `clx` function is built using [clsx](https://www.npmjs.com/package/clsx) and [tw-merge](https://www.npmjs.com/package/tw-merge). It is intended to be used with [Tailwind CSS](https://tailwindcss.com/) to efficiently add classes to your components. |
|
15
|
|
|
16
|
|
|
17
|
`clx` is useful for: |
|
18
|
|
|
19
|
- Conditionally apply classes based on props or state. For example, you can apply the `hidden` class if a component's `open` state variable is `false`. |
|
20
|
- Merge multiple strings into a single class name string. For example, you can apply class names to the component, and allow passing additional class names as props. |
|
21
|
- Override conflicting Tailwind CSS classes. For example, if you specify a `p-2` class name on your component, and you pass a `p-4` class name as a prop, the `p-4` class will take precedence. |
|
22
|
- The last class name specified will take precedence over any previous class names. |
|
23
|
|
|
24
|
For example: |
|
25
|
|
|
26
|
```tsx |
|
27
|
import { clx } from "@medusajs/ui" |
|
28
|
|
|
29
|
type BoxProps = { |
|
30
|
className?: string |
|
31
|
children: React.ReactNode |
|
32
|
mt: "sm" | "md" | "lg" |
|
33
|
} |
|
34
|
|
|
35
|
const Box = ({ className, children, mt }: BoxProps) => { |
|
36
|
return ( |
|
37
|
<div |
|
38
|
className={clx( |
|
39
|
"flex items-center justify-center", |
|
40
|
{ |
|
41
|
"mt-4": mt === "sm", |
|
42
|
"mt-8": mt === "md", |
|
43
|
"mt-12": mt === "lg", |
|
44
|
}, |
|
45
|
className |
|
46
|
)} |
|
47
|
> |
|
48
|
{children} |
|
49
|
</div> |
|
50
|
) |
|
51
|
} |
|
52
|
``` |
|
53
|
|
|
54
|
In the above example, you use `clx` to: |
|
55
|
|
|
56
|
- Apply a base style. |
|
57
|
- Apply a margin top that depends on the `mt` prop. |
|
58
|
- Add class names passed as a prop. |
|
59
|
|
|
60
|
`clx` ensures that Tailwind CSS classes are merged without style conflicts. |
|
61
|
|
|
62
|
--- |
|
63
|
|
|
64
|
## API Reference |
|
65
|
|
|
66
|
### clx Parameters |
|
67
|
|
|
68
|
`clx` accepts any number of arguments, each of them can be of the following types: |
|
69
|
|
|
70
|
- `string`: A string of class names to apply. |
|
71
|
|
|
72
|
```tsx |
|
73
|
clx("flex items-center justify-between") |
|
74
|
``` |
|
75
|
|
|
76
|
- `Record<string, boolean>`: An object whose keys are the class names to apply, and the values are booleans indicating whether to apply the class names. |
|
77
|
|
|
78
|
```tsx |
|
79
|
clx({ |
|
80
|
"flex items-center justify-between": isFlex, |
|
81
|
}) |
|
82
|
``` |
|
83
|
|
|
84
|
- `Array`: An array of strings or objects to apply. |
|
85
|
|
|
86
|
```tsx |
|
87
|
clx([ |
|
88
|
"flex items-center justify-between", |
|
89
|
{ |
|
90
|
"hidden": isHidden, |
|
91
|
}, |
|
92
|
]) |