You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`*.child`, `.sibling`, and `.descendant*` variants for [TailwindCSS v3+](https://tailwindcss.com)
2
5
3
-
<divstyle='font-size:12px'> Repeat elements without repeating styles<br> *.child, .sibling, and .descendant* variants for [TailwindCSS v3+](https://tailwindcss.com). </div>
Same basic usage, but is applied to first of the repeating elements.
70
-
Styles must be applied twice - once for itself and once for siblings with `sibling:` variant. (This has *some* duplication, but has an advantage that the styles are applied directly to the affected element and not to the parent.)
sibling:ring-white sibling-hover:shadow">I have a white ring...</p>
76
-
<p class="sibling">And a shadow on hover!</p>
71
+
<p>I have a white ring...</p>
77
72
</div>
78
-
79
-
<div>
80
-
<p class="
81
-
ring-white hover:shadow
82
-
sibling-p:ring-white hover:sibling-p:shadow">I have a white ring...</p>
73
+
<div>
83
74
<p>And a shadow on hover!</p>
84
75
</div>
76
+
</div>
77
+
```
78
+
79
+
## Sibling variant
80
+
Same basic usage, but is applied to first of the repeating elements.
81
+
Styles must be applied twice - once for itself and once for siblings with `sibling:` variant. (This has *some* duplication, but has an advantage that the styles are applied directly to the affected element and not to the parent)
82
+
83
+
```html
84
+
<div>
85
+
<pclass="
86
+
ring-white hover:shadow
87
+
sibling:ring-white sibling-hover:shadow">I have a white ring...</p>
88
+
<pclass="sibling">And a shadow on hover!</p>
89
+
</div>
90
+
91
+
<div>
92
+
<pclass="
93
+
ring-white hover:shadow
94
+
sibling-p:ring-white hover:sibling-p:shadow">I have a white ring...</p>
95
+
<p>And a shadow on hover!</p>
96
+
</div>
97
+
```
85
98
86
99
# Rationale
87
100
@@ -97,41 +110,46 @@ And its an issue which is surprising, as Tailwind's class based nature should ma
97
110
98
111
This, then, is an attempt to solve the issue of reusing styles through a plugin, and maybe @adam will see this and make it official :)
99
112
100
-
###The Issue
113
+
## The Issue
101
114
102
115
To copy the example loosely from https://tailwindcss.com/docs/reusing-styles.
103
-
Let's say you want to experiment with adding a shadow to all your `<img>s`.
116
+
Let's say you want to experiment with adding a shadow to all your `<img>`s.
104
117
Manually copying it in each one is slowly and buggy. What to do?
@@ -144,30 +162,34 @@ This will make it simple to experiment, easy to understand, is clean and is DRY.
144
162
Even better, we can have `siblings-of-type` to have it only apply said classes to peer elements with same tag.
145
163
Admittedly, I do not know of any similar existing TW rule, but maybe it is just a matter of no need.
146
164
147
-
### Perhaps, a better solution for children (non-standard, not implemented)
165
+
### A better solution for children (non-standard, not implemented)
148
166
149
167
Tailwind doesn't use other custom attributes, but I don't see why they shouldn't. CSS can handle any selectors, and we can prefix with tw- if we worry about conflict.
150
168
151
169
We can therefore create a children attribute with what ought to be applied to the children, and that would readable and DRY.
To be sure, I think that Tailwind should offer this for all psuedo-classes:
167
187
168
-
<div class="block"
169
-
tw-after="content-['wow'] text-white">...
170
-
</div>
188
+
```html
189
+
<divclass="block"
190
+
tw-after="content-['wow'] text-white">...
191
+
</div>
192
+
```
171
193
172
194
And it is my hope that this plugin might be a foray into such usage!
173
195
@@ -177,16 +199,18 @@ And it is my hope that this plugin might be a foray into such usage!
177
199
Like every good project, Tailwind has a consistent style, and we should be consistent with it.
178
200
Here are examples where a TW class effects another element:
179
201
180
-
<body class="dark">
181
-
<p class="dark:shadow">p has shadow when dark theme enabled</p>
182
-
</body>
202
+
```html
203
+
<bodyclass="dark">
204
+
<pclass="dark:shadow">p has shadow when dark theme enabled</p>
205
+
</body>
183
206
184
-
<input class="peer">
185
-
<p class="peer-hover:shadow">p has shadow when hovering on input</p>
207
+
<inputclass="peer">
208
+
<pclass="peer-hover:shadow">p has shadow when hovering on input</p>
186
209
187
-
<div class="group">
188
-
<p class="group-hover:shadow">p has shadow when hovering over div</p>
189
-
</div>
210
+
<divclass="group">
211
+
<pclass="group-hover:shadow">p has shadow when hovering over div</p>
212
+
</div>
213
+
```
190
214
191
215
In each of these examples,
192
216
- we have the class on both the "calling" and "receiving" elements,
@@ -199,42 +223,46 @@ Therefore, in the first version of tailwind-children, we used the form `child-ho
199
223
To match [@tailwindcss/typography], you can now use the form hover:child:shadow or hover:child-div:shadow.
200
224
In addittion, you can still use child-hover:shadow, but not when specifying a element type.
201
225
202
-
Let's see how that works out for us:
226
+
### Let's see how that works out for us:
203
227
204
228
1. When declaring a state, we can use the form `hover:child:shadow` with the hover *before* the child.
205
229
-*Deprecated:* One can also use the form `child-hover:shadow`.
206
230
This form cannot be used with a element type (eg. `child-div-hover:shadow` won't work).
207
231
2. The rules only apply to children that have the matching `child/sibling/etc` class.
208
232
To demonstrate, if wanted only one of two children to match, here are several options.
209
233
210
-
a. Require elements to "opt-in" with a "child" class
211
-
```
234
+
a. Require elements to "opt-in" with a "child" class
235
+
```html
212
236
<div class="child:shadow">
213
237
<p class="child">Shadow</p>
214
238
<p>No Shadow</p>
215
239
</div>
216
240
```
241
+
217
242
b. Apply style to all children, and allow children to override rules as needed.
218
-
```
243
+
```html
219
244
<div class="child:shadow">
220
245
<p>Shadow</p>
221
246
<p class="drop-shadow-none">No Shadow</p>
222
247
</div>
223
248
```
249
+
224
250
c. Offer a no-child class that tells it to not inherit
225
-
```
251
+
```html
226
252
<div class="child:shadow">
227
253
<p>Shadow</p>
228
254
<p class="no-child">No Shadow</p>
229
255
</div>
230
256
```
257
+
231
258
d. Only apply inheritance to element whose type is passed in. (Either `child-span` or `child-['span']`)
232
-
```
259
+
```html
233
260
<div class="child-span:shadow">
234
261
<span>Shadow</span>
235
262
<p>No Shadow</p>
236
263
</div>
237
264
```
265
+
238
266
[tailwind-child] uses method d, and [@tailwindcss/typography] supports methods b, c and d.
239
267
We hope to support all 4, but c is not yet implemented.
240
268
3. No using a class to copy other classes. Excludes the "better sibling solution" above.
@@ -246,6 +274,6 @@ I might eventually implement it, even though it is not a tailwind style rule at
246
274
247
275
Please open issues, file bug reports, give me your opinions on variant names, default styles and behaviors, and whatever else you can think of. There are a lot of good things input can add!
0 commit comments