tailwind-child-cap 1.0.0
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/README.md +89 -0
- package/package.json +22 -0
- package/tailwind-child-cap.js +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# tailwind-child-cap
|
|
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.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
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.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install the plugin via npm:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install -D tailwind-child-cap
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Add `tailwind-child-cap` to your Tailwind CSS configuration file (`tailwind.config.js`):
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
// tailwind.config.js
|
|
25
|
+
module.exports = {
|
|
26
|
+
// ...
|
|
27
|
+
plugins: [
|
|
28
|
+
require("tailwind-child-cap"),
|
|
29
|
+
// ... other plugins
|
|
30
|
+
],
|
|
31
|
+
};
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Optionally, you can customize the maximum range of the child-cap classes:
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
// tailwind.config.js
|
|
38
|
+
module.exports = {
|
|
39
|
+
// ...
|
|
40
|
+
plugins: [
|
|
41
|
+
require("tailwind-child-cap")({ maxRange: 30 }),
|
|
42
|
+
// ...
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Examples
|
|
48
|
+
|
|
49
|
+
### Basic Usage
|
|
50
|
+
|
|
51
|
+
```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>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
In this example, only the first three child elements will be visible.
|
|
62
|
+
|
|
63
|
+
### Responsive Design
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<div class="child-cap-2 md:child-cap-4 lg:child-cap-6">
|
|
67
|
+
<!-- Child elements here -->
|
|
68
|
+
</div>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
This setup displays 2 child elements by default, 4 on medium screens, and 6 on large screens.
|
|
72
|
+
|
|
73
|
+
### Removing the Cap
|
|
74
|
+
|
|
75
|
+
```html
|
|
76
|
+
<div class="md:child-cap-3 lg:child-cap-none">
|
|
77
|
+
<!-- Child elements here -->
|
|
78
|
+
</div>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
This configuration caps the children to 3 on medium screens and removes the cap on large screens, displaying all children.
|
|
82
|
+
|
|
83
|
+
## Contributing
|
|
84
|
+
|
|
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.
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
This plugin is licensed under the MIT License. See [LICENSE](LICENSE) for more information.
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tailwind-child-cap",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Tailwind CSS plugin to control the maximum number of visible child elements in a container.",
|
|
5
|
+
"main": "tailwind-child-cap.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"tailwindcss": "^3.0.0"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"tailwindcss",
|
|
14
|
+
"tailwind",
|
|
15
|
+
"plugin",
|
|
16
|
+
"layout",
|
|
17
|
+
"visibility",
|
|
18
|
+
"children"
|
|
19
|
+
],
|
|
20
|
+
"author": "Swen de Haan",
|
|
21
|
+
"license": "MIT"
|
|
22
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
const plugin = require("tailwindcss/plugin");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Tailwind CSS plugin to control the maximum number of visible child elements in a container.
|
|
6
|
+
* This plugin generates utility classes in the form of `.child-cap-{n}`, where `{n}` is the number of child elements to display,
|
|
7
|
+
* and `.child-cap-none` to display all child elements.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} options - Optional. Configuration options for the plugin.
|
|
10
|
+
*/
|
|
11
|
+
const childCapPlugin = plugin(function ({ addUtilities, e, theme }, options = {}) {
|
|
12
|
+
const maxRange = options.maxRange || 24;
|
|
13
|
+
const screens = theme("screens", {});
|
|
14
|
+
|
|
15
|
+
let newUtilities = {};
|
|
16
|
+
|
|
17
|
+
// Function to generate utilities for a given selector.
|
|
18
|
+
const generateUtilities = (selectorPrefix, maxItems) => {
|
|
19
|
+
for (let i = 0; i <= maxItems; i++) {
|
|
20
|
+
const selector = `${selectorPrefix}${i} > *`;
|
|
21
|
+
const visibleSelector = `${selectorPrefix}${i} > :nth-child(-n+${i})`;
|
|
22
|
+
newUtilities[selector] = { display: "none" };
|
|
23
|
+
newUtilities[visibleSelector] = { display: "block" };
|
|
24
|
+
}
|
|
25
|
+
newUtilities[`${selectorPrefix}none > *`] = { display: "block" };
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Generate base utilities for all ranges.
|
|
29
|
+
generateUtilities(".child-cap-", maxRange);
|
|
30
|
+
|
|
31
|
+
// Generate responsive utilities for each breakpoint.
|
|
32
|
+
Object.keys(screens).forEach((screen) => {
|
|
33
|
+
const screenClassPrefix = `.${e(`${screen}:child-cap-`)}`;
|
|
34
|
+
generateUtilities(screenClassPrefix, maxRange);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
addUtilities(newUtilities, ["responsive"]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
module.exports = childCapPlugin;
|