Skip to content

Commit 03b3018

Browse files
authored
Merge pull request #30 from MichaelWest22/patch-1
Add apply-parent-classes feature to class-tools
2 parents c5a7374 + 90b1495 commit 03b3018

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

src/class-tools/README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ Within a run, a `,` character separates distinct class operations.
1212
A class operation is an operation name `add`, `remove`, or `toggle`, followed by a CSS class name,
1313
optionally followed by a colon `:` and a time delay.
1414

15+
There is also the option to use `apply-parent-classes` or `data-apply-parent-classes` which uses the same format as `classes` but is instead designed for Out of band updates to allow you to manipulate CSS classes of an existing element in the DOM without otherwise knowing or altering its state. Any element with this property will apply classes to its parent and also remove this child element afterwards so should ideally be used as part of a `hx-swap-oob="beforeend: #some-element`.
16+
1517
## Install
1618

1719
```html
18-
<script src="https://unpkg.com/[email protected].0/class-tools.js"></script>
20+
<script src="https://unpkg.com/[email protected].1/class-tools.js"></script>
1921
```
2022

2123
## Usage
@@ -30,4 +32,7 @@ optionally followed by a colon `:` and a time delay.
3032
class "foo" after 1s -->
3133
<div classes="toggle foo:1s"/> <!-- toggles the class "foo" every 1s -->
3234
</div>
35+
<div hx-swap-oob="beforeend: #my-element"> <!-- adds the class "foo" to my-element for 10s -->
36+
<div hx-ext="class-tools" apply-parent-classes="add foo, remove foo:10s"></div>
37+
</div>
3338
```

src/class-tools/class-tools.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@
7979
if (name === 'htmx:afterProcessNode') {
8080
var elt = evt.detail.elt
8181
maybeProcessClasses(elt)
82-
if (elt.querySelectorAll) {
82+
var classList = elt.getAttribute("apply-parent-classes") || elt.getAttribute("data-apply-parent-classes");
83+
if (classList) {
84+
var parent = elt.parentElement;
85+
parent.removeChild(elt);
86+
parent.setAttribute("classes", classList);
87+
maybeProcessClasses(parent);
88+
} else if (elt.querySelectorAll) {
8389
var children = elt.querySelectorAll('[classes], [data-classes]')
8490
for (var i = 0; i < children.length; i++) {
8591
maybeProcessClasses(children[i])

src/class-tools/package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/class-tools/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "htmx-ext-class-tools",
33
"main": "class-tools.js",
4-
"version": "2.0.0",
4+
"version": "2.0.1",
55
"scripts": {
66
"lint": "eslint test/ext test",
77
"lint-fix": "eslint test/ext test --fix",

src/class-tools/test/ext/class-tools.js

+28
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ describe('class-tools extension', function() {
3737
}, 100)
3838
})
3939

40+
it('adds classes to parent properly', function(done) {
41+
var div = make('<div>Click Me!<div hx-ext="class-tools" apply-parent-classes="add c1"></div></div>')
42+
should.equal(div.classList.length, 0)
43+
setTimeout(function() {
44+
should.equal(div.classList.contains('c1'), true)
45+
done()
46+
}, 100)
47+
})
48+
49+
it('removes classes from parent properly', function(done) {
50+
var div = make('<div class="foo bar">Click Me!<div hx-ext="class-tools" apply-parent-classes="remove bar"></div></div>')
51+
should.equal(div.classList.contains('foo'), true)
52+
should.equal(div.classList.contains('bar'), true)
53+
setTimeout(function() {
54+
should.equal(div.classList.contains('foo'), true)
55+
should.equal(div.classList.contains('bar'), false)
56+
done()
57+
}, 100)
58+
})
59+
60+
it('cleans up child with apply-parent-classes properly', function(done) {
61+
var div = make('<div class="foo bar">Click Me!<div id="d2" hx-ext="class-tools" apply-parent-classes="remove bar"></div></div>')
62+
setTimeout(function() {
63+
should.not.exist(byId('d2'))
64+
done()
65+
}, 100)
66+
})
67+
4068
it('extension can be on parent', function(done) {
4169
var div = make('<div hx-ext="class-tools"><div id="d1" classes="add c1">Click Me!</div></div>')
4270
should.equal(div.classList.length, 0)

0 commit comments

Comments
 (0)