Skip to content

Commit 1dc1023

Browse files
committed
Add improved docs
1 parent cf980b2 commit 1dc1023

File tree

2 files changed

+79
-30
lines changed

2 files changed

+79
-30
lines changed

lib/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ const own = {}.hasOwnProperty
2424
/**
2525
* Merge classes.
2626
*
27+
* This function has two signatures, depending on whether a `node` was passed.
28+
*
2729
* @param node
28-
* Optionally, node whose classes to append to.
30+
* Optionally, node whose classes to append to (should be `Element`).
2931
* @param conditionals
3032
* Class configuration to merge.
3133
* @returns
@@ -34,8 +36,8 @@ const own = {}.hasOwnProperty
3436
export const classnames =
3537
/**
3638
* @type {(
37-
* (<T extends Node>(node: T, ...conditions: Array<Conditional>) => T) &
38-
* ((...conditions: Array<Conditional>) => Array<string>)
39+
* (<T extends Node>(node: T, ...conditionals: Array<Conditional>) => T) &
40+
* ((...conditionals: Array<Conditional>) => Array<string>)
3941
* )}
4042
*/
4143
(

readme.md

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[hast][] utility to transform to set classes.
11+
[hast][] utility to set classes.
1212

1313
## Contents
1414

@@ -17,8 +17,9 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`classnames(node, …conditionals)`](#classnamesnode-conditionals)
21-
* [`classnames(…conditionals)`](#classnamesconditionals)
20+
* [`classnames([node, ]…conditionals)`](#classnamesnode-conditionals)
21+
* [`ConditionalPrimitive`](#conditionalprimitive)
22+
* [`ConditionalMap`](#conditionalmap)
2223
* [`Conditional`](#conditional)
2324
* [Types](#types)
2425
* [Compatibility](#compatibility)
@@ -40,7 +41,7 @@ a lot when working with classes in the syntax tree.
4041
## Install
4142

4243
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][]:
4445

4546
```sh
4647
npm install hast-util-classnames
@@ -86,45 +87,83 @@ Yields:
8687

8788
## API
8889

89-
This package exports the identifier `classnames`.
90+
This package exports the identifier [`classnames`][classnames].
9091
There is no default export.
9192

92-
### `classnames(node, …conditionals)`
93+
### `classnames([node, ]…conditionals)`
9394

94-
Utility to merge classes.
95+
Merge classes.
9596

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.
10098

101-
### `classnames(…conditionals)`
99+
###### Signatures
102100

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>`
105103

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`
107127
108-
A value that is either:
128+
Map of class names as keys, with whether they’re turned on or not as values.
109129
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+
```
116154
117155
## Types
118156
119157
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].
122161
123162
## Compatibility
124163
125164
Projects maintained by the unified collective are compatible with all maintained
126165
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+.
128167
Our projects sometimes work with older versions, but this is not guaranteed.
129168
130169
## Security
@@ -133,7 +172,7 @@ Classes are typically not harmful, however, if someone were able to inject
133172
classes, it could mean that user-provided content looks like official content,
134173
which may cause security problems due to impersonation.
135174
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.
137176
138177
## Related
139178
@@ -210,8 +249,16 @@ abide by its terms.
210249
211250
[hast]: https://github.com/syntax-tree/hast
212251
252+
[node]: https://github.com/syntax-tree/hast#nodes
253+
213254
[element]: https://github.com/syntax-tree/hast#element
214255
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
216259
217260
[conditional]: #conditional
261+
262+
[conditionalmap]: #conditionalmap
263+
264+
[conditionalprimitive]: #conditionalprimitive

0 commit comments

Comments
 (0)