tailwind-child-cap 2.0.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -48
- package/package.json +27 -4
- package/tailwind-child-cap.js +61 -30
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Swen de Haan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,89 +1,109 @@
|
|
|
1
1
|
# tailwind-child-cap
|
|
2
2
|
|
|
3
|
-
`tailwind-child-cap` is a Tailwind CSS plugin
|
|
3
|
+
`tailwind-child-cap` is a Tailwind CSS plugin that limits how many direct child elements are visible in a container.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- Generates
|
|
8
|
-
-
|
|
9
|
-
-
|
|
7
|
+
- Generates `.child-cap-1` to `.child-cap-N` utilities
|
|
8
|
+
- Includes `.child-cap-none` to remove the cap
|
|
9
|
+
- Works with responsive variants (`sm:`, `md:`, `lg:`, and so on)
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
13
|
-
Install the plugin via npm:
|
|
14
|
-
|
|
15
13
|
```bash
|
|
16
|
-
npm install -
|
|
14
|
+
npm install --save-dev tailwind-child-cap
|
|
17
15
|
```
|
|
18
16
|
|
|
19
|
-
## Usage
|
|
17
|
+
## Usage with Tailwind 4 (`app.css`)
|
|
20
18
|
|
|
21
|
-
|
|
19
|
+
```css
|
|
20
|
+
@import "tailwindcss";
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
@plugin "tailwind-child-cap";
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Custom range:
|
|
26
|
+
|
|
27
|
+
```css
|
|
28
|
+
@import "tailwindcss";
|
|
29
|
+
|
|
30
|
+
@plugin "tailwind-child-cap" {
|
|
31
|
+
maxrange: 42;
|
|
32
|
+
}
|
|
32
33
|
```
|
|
33
34
|
|
|
34
|
-
|
|
35
|
+
## Usage with `tailwind.config.js`
|
|
35
36
|
|
|
36
|
-
```
|
|
37
|
-
// tailwind.config.js
|
|
37
|
+
```js
|
|
38
38
|
module.exports = {
|
|
39
|
-
|
|
40
|
-
plugins: [
|
|
41
|
-
require("tailwind-child-cap")({ maxRange: 30 }),
|
|
42
|
-
// ...
|
|
43
|
-
],
|
|
39
|
+
plugins: [require("tailwind-child-cap")({ maxRange: 30 })],
|
|
44
40
|
};
|
|
45
41
|
```
|
|
46
42
|
|
|
43
|
+
## Options
|
|
44
|
+
|
|
45
|
+
Use `maxRange` in JavaScript config or `maxrange` in the Tailwind 4 `@plugin` block.
|
|
46
|
+
|
|
47
|
+
- Type: positive integer
|
|
48
|
+
- Default: `24`
|
|
49
|
+
- Invalid values (non-number, `0`, negative) fall back to `24`
|
|
50
|
+
|
|
47
51
|
## Examples
|
|
48
52
|
|
|
49
|
-
### Basic
|
|
53
|
+
### Basic usage
|
|
50
54
|
|
|
51
55
|
```html
|
|
52
|
-
<
|
|
53
|
-
<
|
|
54
|
-
<
|
|
55
|
-
<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
</
|
|
56
|
+
<ul class="child-cap-3">
|
|
57
|
+
<li>Item 1</li>
|
|
58
|
+
<li>Item 2</li>
|
|
59
|
+
<li>Item 3</li>
|
|
60
|
+
<!-- Hidden from here -->
|
|
61
|
+
<li>Item 4</li>
|
|
62
|
+
<li>Item 5</li>
|
|
63
|
+
</ul>
|
|
59
64
|
```
|
|
60
65
|
|
|
61
|
-
In this example, only the first three child elements
|
|
66
|
+
In this example, only the first three child elements are visible.
|
|
62
67
|
|
|
63
|
-
### Responsive
|
|
68
|
+
### Responsive design
|
|
64
69
|
|
|
65
70
|
```html
|
|
66
|
-
<
|
|
67
|
-
|
|
68
|
-
</
|
|
71
|
+
<ul class="child-cap-2 md:child-cap-4 lg:child-cap-6">
|
|
72
|
+
<li>Item 1</li>
|
|
73
|
+
<li>Item 2</li>
|
|
74
|
+
<!-- Hidden until md -->
|
|
75
|
+
<li>Item 3</li>
|
|
76
|
+
<li>Item 4</li>
|
|
77
|
+
<!-- Hidden until lg -->
|
|
78
|
+
<li>Item 5</li>
|
|
79
|
+
<li>Item 6</li>
|
|
80
|
+
<!-- Hidden at all breakpoints -->
|
|
81
|
+
<li>Item 7</li>
|
|
82
|
+
</ul>
|
|
69
83
|
```
|
|
70
84
|
|
|
71
|
-
This setup
|
|
85
|
+
This setup shows 2 items by default, 4 on `md`, and 6 on `lg`.
|
|
72
86
|
|
|
73
|
-
### Removing the
|
|
87
|
+
### Removing the cap
|
|
74
88
|
|
|
75
89
|
```html
|
|
76
|
-
<
|
|
77
|
-
|
|
78
|
-
</
|
|
90
|
+
<ul class="md:child-cap-3 lg:child-cap-none">
|
|
91
|
+
<li>Item 1</li>
|
|
92
|
+
<li>Item 2</li>
|
|
93
|
+
<li>Item 3</li>
|
|
94
|
+
<!-- Hidden on md, visible again on lg -->
|
|
95
|
+
<li>Item 4</li>
|
|
96
|
+
<li>Item 5</li>
|
|
97
|
+
</ul>
|
|
79
98
|
```
|
|
80
99
|
|
|
81
|
-
|
|
100
|
+
On medium screens this caps at 3 items, and on large screens the cap is removed.
|
|
82
101
|
|
|
83
|
-
##
|
|
102
|
+
## Compatibility
|
|
84
103
|
|
|
85
|
-
|
|
104
|
+
- Tailwind CSS: `^4.0.0`
|
|
105
|
+
- Node.js: `>=14`
|
|
86
106
|
|
|
87
107
|
## License
|
|
88
108
|
|
|
89
|
-
|
|
109
|
+
MIT, see [LICENSE](LICENSE).
|
package/package.json
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tailwind-child-cap",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "A Tailwind CSS plugin to control the maximum number of visible child elements in a container.",
|
|
5
5
|
"main": "tailwind-child-cap.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./tailwind-child-cap.js",
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"tailwind-child-cap.js",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
6
15
|
"scripts": {
|
|
7
|
-
"
|
|
16
|
+
"lint": "node --check tailwind-child-cap.js && node --check test/tailwind-child-cap.test.js",
|
|
17
|
+
"test": "node test/tailwind-child-cap.test.js",
|
|
18
|
+
"prepack": "npm run lint && npm test"
|
|
8
19
|
},
|
|
9
20
|
"peerDependencies": {
|
|
10
21
|
"tailwindcss": "^4.0.0"
|
|
11
22
|
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"tailwindcss": "^4.0.3"
|
|
25
|
+
},
|
|
12
26
|
"keywords": [
|
|
13
27
|
"tailwindcss",
|
|
14
28
|
"tailwind",
|
|
@@ -21,9 +35,18 @@
|
|
|
21
35
|
"license": "MIT",
|
|
22
36
|
"repository": {
|
|
23
37
|
"type": "git",
|
|
24
|
-
"url": "https://github.com/
|
|
38
|
+
"url": "git+https://github.com/Swennet/tailwind-child-cap.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/Swennet/tailwind-child-cap/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/Swennet/tailwind-child-cap#readme",
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public",
|
|
46
|
+
"provenance": true
|
|
25
47
|
},
|
|
26
48
|
"engines": {
|
|
27
49
|
"node": ">=14.0.0"
|
|
28
|
-
}
|
|
50
|
+
},
|
|
51
|
+
"sideEffects": false
|
|
29
52
|
}
|
package/tailwind-child-cap.js
CHANGED
|
@@ -1,36 +1,67 @@
|
|
|
1
|
-
const plugin = require(
|
|
1
|
+
const plugin = require("tailwindcss/plugin");
|
|
2
|
+
|
|
3
|
+
const DEFAULT_MAX_RANGE = 24;
|
|
4
|
+
|
|
5
|
+
function normalizeOptions(options) {
|
|
6
|
+
if (!options || typeof options !== "object" || Array.isArray(options)) {
|
|
7
|
+
return {};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return Object.fromEntries(
|
|
11
|
+
Object.entries(options).map(([key, value]) => [
|
|
12
|
+
key.toLowerCase().replace(/[-_]/g, ""),
|
|
13
|
+
value,
|
|
14
|
+
]),
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function resolveMaxRange(options = {}) {
|
|
19
|
+
const normalized = normalizeOptions(options);
|
|
20
|
+
const raw = normalized.maxrange ?? DEFAULT_MAX_RANGE;
|
|
21
|
+
const parsed = Number.parseInt(String(raw).trim(), 10);
|
|
22
|
+
|
|
23
|
+
if (!Number.isFinite(parsed) || parsed < 1) {
|
|
24
|
+
return DEFAULT_MAX_RANGE;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return parsed;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function createUtilities(maxRange) {
|
|
31
|
+
const utilities = {};
|
|
32
|
+
|
|
33
|
+
for (let i = 1; i <= maxRange; i++) {
|
|
34
|
+
utilities[`.child-cap-${i}`] = {
|
|
35
|
+
"& > *": {
|
|
36
|
+
display: "none",
|
|
37
|
+
},
|
|
38
|
+
[`& > :where(:nth-child(-n+${i}))`]: {
|
|
39
|
+
display: "block",
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
utilities[".child-cap-none"] = {
|
|
45
|
+
"& > *": {display: "block"},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return utilities;
|
|
49
|
+
}
|
|
2
50
|
|
|
3
51
|
const childCapPlugin = plugin.withOptions(
|
|
4
52
|
(options = {}) =>
|
|
5
|
-
({
|
|
6
|
-
const maxRange = options
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// Base utilities
|
|
10
|
-
for (let i = 1; i <= maxRange; i++) {
|
|
11
|
-
utilities[`.child-cap-${i}`] = {
|
|
12
|
-
'& > *': {
|
|
13
|
-
display: 'none',
|
|
14
|
-
[`&:nth-child(-n+${i})`]: {
|
|
15
|
-
display: 'block'
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// No-cap utility
|
|
22
|
-
utilities[`.child-cap-none`] = {
|
|
23
|
-
'& > *': {
|
|
24
|
-
display: 'block'
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
addUtilities(utilities, {
|
|
53
|
+
({addUtilities}) => {
|
|
54
|
+
const maxRange = resolveMaxRange(options);
|
|
55
|
+
addUtilities(createUtilities(maxRange), {
|
|
29
56
|
respectPrefix: false,
|
|
30
57
|
respectImportant: false,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
)
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
);
|
|
35
61
|
|
|
36
|
-
module.exports = childCapPlugin
|
|
62
|
+
module.exports = childCapPlugin;
|
|
63
|
+
module.exports._private = {
|
|
64
|
+
DEFAULT_MAX_RANGE,
|
|
65
|
+
createUtilities,
|
|
66
|
+
resolveMaxRange,
|
|
67
|
+
};
|