8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ hast] [ ] utility to transform to set classes.
11
+ [ hast] [ ] utility to set classes.
12
12
13
13
## Contents
14
14
17
17
* [ Install] ( #install )
18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
- * [ ` classnames(node, …conditionals) ` ] ( #classnamesnode-conditionals )
21
- * [ ` classnames(…conditionals) ` ] ( #classnamesconditionals )
20
+ * [ ` classnames([node, ]…conditionals) ` ] ( #classnamesnode-conditionals )
21
+ * [ ` ConditionalPrimitive ` ] ( #conditionalprimitive )
22
+ * [ ` ConditionalMap ` ] ( #conditionalmap )
22
23
* [ ` Conditional ` ] ( #conditional )
23
24
* [ Types] ( #types )
24
25
* [ Compatibility] ( #compatibility )
@@ -40,7 +41,7 @@ a lot when working with classes in the syntax tree.
40
41
## Install
41
42
42
43
This package is [ ESM only] [ esm ] .
43
- In Node.js (version 12.20+, 14.14+, 16.0+, 18 .0+), install with [ npm] [ ] :
44
+ In Node.js (version 14.14+ and 16 .0+), install with [ npm] [ ] :
44
45
45
46
``` sh
46
47
npm install hast-util-classnames
@@ -86,45 +87,83 @@ Yields:
86
87
87
88
## API
88
89
89
- This package exports the identifier ` classnames ` .
90
+ This package exports the identifier [ ` classnames ` ] [ classnames ] .
90
91
There is no default export.
91
92
92
- ### ` classnames(node, …conditionals) `
93
+ ### ` classnames([ node, ] …conditionals) `
93
94
94
- Utility to merge classes.
95
+ Merge classes.
95
96
96
- If the first argument is a node it should be an [ element] [ ] .
97
- All [ conditionals] [ conditional ] are merged with the current class names and with
98
- each other, and then set on the element.
99
- Finally, the given ` node ` is returned.
97
+ This function has two signatures, depending on whether a ` node ` was passed.
100
98
101
- ### ` classnames(…conditionals) `
99
+ ###### Signatures
102
100
103
- If the first argument is not a node, all [ conditionals] [ conditional ] are merged
104
- with each other, and then the resulting array is returned.
101
+ * ` ( node: Node, ... conditionals: Array<Conditional>) => Node `
102
+ * ` (...conditionals: Array<Conditional>) => Array<string> `
105
103
106
- ### ` Conditional `
104
+ ###### Parameters
105
+
106
+ * ` node ` ([ ` Node ` ] [ node ] )
107
+ — optionally, node whose classes to append to (should be
108
+ [ ` Element ` ] [ element ] )
109
+ * ` conditionals ` ([ ` Array<Conditional> ` ] [ conditional ] )
110
+ — class configuration to merge
111
+
112
+ ###### Returns
113
+
114
+ The given node ([ ` Node ` ] [ node ] ), if any, or a list of strings (` Array<string> ` ).
115
+
116
+ ### ` ConditionalPrimitive `
117
+
118
+ Basic class names (TypeScript type).
119
+
120
+ ###### Type
121
+
122
+ ``` ts
123
+ type ConditionalPrimitive = string | number
124
+ ` ` `
125
+
126
+ ### ` ConditionalMap `
107
127
108
- A value that is either:
128
+ Map of class names as keys, with whether they’re turned on or not as values.
109
129
110
- * ` string ` — one or more space-separated tokens (example: ` alpha bravo ` )
111
- * ` number ` — single token that is cast to string (example: ` 123 ` )
112
- * ` Record<string, boolean> ` — map where each field is a token, and each value
113
- turns it either on or off
114
- * ` Array<Conditional> ` — list of more conditionals
115
- * other values are ignored
130
+ ###### Type
131
+
132
+ ` ` ` ts
133
+ type ConditionalMap = Record <string , boolean >
134
+ ` ` `
135
+
136
+ ### ` Conditional `
137
+
138
+ Different ways to turn class names on or off (TypeScript type).
139
+
140
+ ###### Type
141
+
142
+ ` ` ` ts
143
+ type Conditional =
144
+ | null
145
+ | undefined
146
+ | ConditionalPrimitive
147
+ | ConditionalMap
148
+ | Array <
149
+ | ConditionalPrimitive
150
+ | ConditionalMap
151
+ | Array <ConditionalPrimitive | ConditionalMap >
152
+ >
153
+ ` ` `
116
154
117
155
## Types
118
156
119
157
This package is fully typed with [TypeScript][].
120
- It exports the additional types ` ConditionalPrimitive ` (` string ` /` number ` form),
121
- ` ConditionalMap ` (object form), and ` Conditional ` (any form).
158
+ It exports the additional types [ ` Conditional ` ][conditional],
159
+ [ ` ConditionalMap ` ][conditionalmap], and
160
+ [ ` ConditionalPrimitive ` ][conditionalprimitive].
122
161
123
162
## Compatibility
124
163
125
164
Projects maintained by the unified collective are compatible with all maintained
126
165
versions of Node.js.
127
- As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18 .0+.
166
+ As of now, that is Node.js 14.14+ and 16 .0+.
128
167
Our projects sometimes work with older versions, but this is not guaranteed.
129
168
130
169
## Security
@@ -133,7 +172,7 @@ Classes are typically not harmful, however, if someone were able to inject
133
172
classes, it could mean that user-provided content looks like official content,
134
173
which may cause security problems due to impersonation.
135
174
Either do not use user input in ` classnames ` or use
136
- [ ` hast-util-sanitize ` ] [ sanitize ] to clean the tree.
175
+ [ ` hast -util -sanitize ` ][hast-util- sanitize] to clean the tree.
137
176
138
177
## Related
139
178
@@ -210,8 +249,16 @@ abide by its terms.
210
249
211
250
[hast]: https://github.com/syntax-tree/hast
212
251
252
+ [node]: https://github.com/syntax-tree/hast#nodes
253
+
213
254
[element]: https://github.com/syntax-tree/hast#element
214
255
215
- [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
256
+ [hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize
257
+
258
+ [classnames]: #classnamesnode-conditionals
216
259
217
260
[conditional]: #conditional
261
+
262
+ [conditionalmap]: #conditionalmap
263
+
264
+ [conditionalprimitive]: #conditionalprimitive
0 commit comments