Skip to content

Commit b6b8ab2

Browse files
committed
Accept stores as className state.
1 parent 3d59c61 commit b6b8ab2

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@voxelstack/ceres",
3-
"version": "0.1.7",
3+
"version": "0.2.0",
44
"description": "Web development for me.",
55
"keywords": ["UI", "framework"],
66
"homepage": "https://github.com/voxelstack/ceres",

src/lib/component.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BoundOrRaw, ComponentBinds, documentBinders, getComponentBinders, rawBind, windowBinders } from "./bind";
22
import { EventHandler, toConfiguredListener } from "./event";
3-
import { Actions, Attributes, BodyProxyProps, Classes, DocumentProxyProps, Handlers, Props, Reactive, Stringifiable, Styles, Tag, WindowProxyProps } from "./props";
3+
import { Actions, Attributes, BodyProxyProps, Classes, DocumentProxyProps, Handlers, LiteralOrStore, Props, Reactive, Stringifiable, Styles, Tag, WindowProxyProps } from "./props";
44
import { ReactiveString } from "./reactive_string";
55
import { Child, Disposable, Renderable } from "./renderable";
66
import { $derive, AtomStore, Store, StoredTypes, ValueCallback } from "./store";
@@ -293,25 +293,16 @@ function attachClass(element: HTMLElement, className?: Classes) {
293293
element.classList.remove(c);
294294
});
295295
} else {
296-
const prev: Record<string, boolean> = previous ?? {};
297296
Object.entries(next).forEach(([clazz, enabled]) => {
298-
if (enabled) {
299-
if (!prev[clazz]) {
300-
element.classList.add(clazz);
301-
} else {
302-
delete prev[clazz];
303-
}
304-
} else {
305-
if (prev[clazz]) {
297+
const dispose = watchProp(enabled, (isEnabled, wasEnabled) => {
298+
if (wasEnabled && !isEnabled) {
306299
element.classList.remove(clazz);
307-
} else {
308-
delete prev[clazz];
300+
} else if (isEnabled && !wasEnabled) {
301+
element.classList.add(clazz);
309302
}
310-
}
311-
});
312-
Object.entries(prev).forEach(([clazz, enabled]) => {
313-
if (enabled) {
314-
element.classList.remove(clazz);
303+
});
304+
if (dispose) {
305+
disposables.push(dispose);
315306
}
316307
});
317308
}

src/lib/props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export type Styles = Partial<{
5656
export type Classes =
5757
| LiteralOrStore<Stringifiable>
5858
| LiteralOrStore<Array<string>>
59-
| LiteralOrStore<Record<string, boolean>>
59+
| Record<string, LiteralOrStore<boolean>>
6060
;
6161

6262
// https://github.com/microsoft/TypeScript/issues/40689

0 commit comments

Comments
 (0)