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 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 designed to control the maximum number of visible child elements within a container. This utility is particularly useful for creating responsive designs where the number of displayed elements needs to be adjusted based on the screen size.
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 utility classes `.child-cap-{n}` to specify the number of visible child elements.
8
- - Provides `.child-cap-none` to display all child elements, overriding previous cap settings.
9
- - Fully responsive, allowing different caps at different breakpoints.
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 -D tailwind-child-cap
14
+ npm install --save-dev tailwind-child-cap
17
15
  ```
18
16
 
19
- ## Usage
17
+ ## Usage with Tailwind 4 (`app.css`)
20
18
 
21
- Add `tailwind-child-cap` to your Tailwind CSS configuration file (`tailwind.config.js`):
19
+ ```css
20
+ @import "tailwindcss";
22
21
 
23
- ```javascript
24
- // tailwind.config.js
25
- module.exports = {
26
- // ...
27
- plugins: [
28
- require("tailwind-child-cap"),
29
- // ... other plugins
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
- Optionally, you can customize the maximum range of the child-cap classes:
35
+ ## Usage with `tailwind.config.js`
35
36
 
36
- ```javascript
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 Usage
53
+ ### Basic usage
50
54
 
51
55
  ```html
52
- <div class="child-cap-3">
53
- <div class="item">Item 1</div>
54
- <div class="item">Item 2</div>
55
- <div class="item">Item 3</div>
56
- <div class="item">Item 4</div>
57
- <!-- Hidden -->
58
- </div>
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 will be visible.
66
+ In this example, only the first three child elements are visible.
62
67
 
63
- ### Responsive Design
68
+ ### Responsive design
64
69
 
65
70
  ```html
66
- <div class="child-cap-2 md:child-cap-4 lg:child-cap-6">
67
- <!-- Child elements here -->
68
- </div>
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 displays 2 child elements by default, 4 on medium screens, and 6 on large screens.
85
+ This setup shows 2 items by default, 4 on `md`, and 6 on `lg`.
72
86
 
73
- ### Removing the Cap
87
+ ### Removing the cap
74
88
 
75
89
  ```html
76
- <div class="md:child-cap-3 lg:child-cap-none">
77
- <!-- Child elements here -->
78
- </div>
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
- This configuration caps the children to 3 on medium screens and removes the cap on large screens, displaying all children.
100
+ On medium screens this caps at 3 items, and on large screens the cap is removed.
82
101
 
83
- ## Contributing
102
+ ## Compatibility
84
103
 
85
- Contributions to `tailwind-child-cap` are welcome! Feel free to submit issues or pull requests on GitHub for any bugs, improvements, or new features.
104
+ - Tailwind CSS: `^4.0.0`
105
+ - Node.js: `>=14`
86
106
 
87
107
  ## License
88
108
 
89
- This plugin is licensed under the MIT License. See [LICENSE](LICENSE) for more information.
109
+ MIT, see [LICENSE](LICENSE).
package/package.json CHANGED
@@ -1,14 +1,28 @@
1
1
  {
2
2
  "name": "tailwind-child-cap",
3
- "version": "2.0.0",
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
- "test": "echo \"Error: no test specified\" && exit 1"
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/your-username/tailwind-child-cap.git"
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
  }
@@ -1,36 +1,67 @@
1
- const plugin = require('tailwindcss/plugin')
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
- ({ addUtilities }) => {
6
- const maxRange = options.maxRange || 24
7
- const utilities = {}
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
- variants: ['responsive']
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
+ };