Skip to content

Commit 03a7f91

Browse files
authored
Updates for RC.6 (#22)
1 parent f7aaaea commit 03a7f91

File tree

2 files changed

+22
-58
lines changed

2 files changed

+22
-58
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ requires = [ "hatchling" ]
55

66
[project]
77
name = "datastar-py"
8-
version = "0.6.5"
8+
version = "0.7.0"
99
description = "Helper functions and classes for the Datastar library (https://data-star.dev/)"
1010
readme = "README.md"
1111
keywords = [

src/datastar_py/attributes.py

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
from collections.abc import Iterable, Iterator, Mapping
66
from itertools import chain
7-
from typing import TYPE_CHECKING, Literal, TypeAlias, Union, overload
7+
from typing import TYPE_CHECKING, Literal, TypeAlias, Union
88

99
if TYPE_CHECKING:
1010
from typing import Self
@@ -170,54 +170,18 @@ def class_(self, class_dict: Mapping | None = None, /, **classes: str) -> BaseAt
170170
classes = {**(class_dict if class_dict else {}), **classes}
171171
return BaseAttr("class", value=_js_object(classes), alias=self._alias)
172172

173-
@overload
174-
def on(self, event: Literal["interval"], expression: str) -> OnIntervalAttr: ...
175-
@overload
176-
def on(self, event: Literal["load"], expression: str) -> OnLoadAttr: ...
177-
@overload
178-
def on(self, event: Literal["intersect"], expression: str) -> OnIntersectAttr: ...
179-
@overload
180-
def on(self, event: Literal["raf"], expression: str) -> OnRafAttr: ...
181-
@overload
182-
def on(self, event: Literal["resize"], expression: str) -> OnResizeAttr: ...
183-
@overload
184-
def on(self, event: Literal["signal-patch"], expression: str) -> OnSignalPatchAttr: ...
185-
@overload
186-
def on(self, event: JSEvent | str, expression: str) -> OnAttr: ...
187-
def on(
188-
self, event: str, expression: str
189-
) -> (
190-
OnAttr
191-
| OnIntervalAttr
192-
| OnLoadAttr
193-
| OnIntersectAttr
194-
| OnRafAttr
195-
| OnResizeAttr
196-
| OnSignalPatchAttr
197-
):
173+
def init(self, expression: str) -> InitAttr:
174+
"""Execute an expression when the element is loaded into the DOM."""
175+
return InitAttr(value=expression, alias=self._alias)
176+
177+
def on(self, event: JSEvent | str, expression: str) -> OnAttr:
198178
"""Execute an expression when an event occurs."""
199-
if event == "interval":
200-
return OnIntervalAttr(value=expression, alias=self._alias)
201-
if event == "load":
202-
return OnLoadAttr(value=expression, alias=self._alias)
203-
if event == "raf":
204-
return OnRafAttr(value=expression, alias=self._alias)
205-
if event == "resize":
206-
return OnResizeAttr(value=expression, alias=self._alias)
207-
if event == "intersect":
208-
return OnIntersectAttr(value=expression, alias=self._alias)
209-
if event == "signal-patch":
210-
return OnSignalPatchAttr(value=expression, alias=self._alias)
211179
return OnAttr(key=event, value=expression, alias=self._alias)
212180

213181
def on_interval(self, expression: str) -> OnIntervalAttr:
214182
"""Execute an expression at a regular interval."""
215183
return OnIntervalAttr(value=expression, alias=self._alias)
216184

217-
def on_load(self, expression: str) -> OnLoadAttr:
218-
"""Execute an expression when the element is loaded into the DOM."""
219-
return OnLoadAttr(value=expression, alias=self._alias)
220-
221185
def on_intersect(self, expression: str) -> OnIntersectAttr:
222186
"""Execute an expression when the element intersects with the viewport."""
223187
return OnIntersectAttr(value=expression, alias=self._alias)
@@ -333,17 +297,16 @@ def __call__(self) -> Self:
333297
def _full_key(self) -> str:
334298
key = f"{self._alias}{self._attr}"
335299
if self._key:
336-
key += f"-{self._key}"
300+
key += f":{self._key}"
337301
for mod, values in self._mods.items():
338302
key += f"__{mod}"
339303
if values:
340304
key += f".{'.'.join(values)}"
341305
return key
342306

343307
def _to_kebab_key(self, key_name: str) -> None:
344-
if "-" in key_name:
345-
kebab_name, from_case = key_name.lower(), "kebab"
346-
elif "_" in key_name:
308+
if "__" in key_name:
309+
# _ are allowed in attributes, the only time we need to convert is if there are multiple underscores
347310
kebab_name, from_case = key_name.lower().replace("_", "-"), "snake"
348311
elif key_name[0].isupper():
349312
kebab_name, from_case = (
@@ -356,7 +319,8 @@ def _to_kebab_key(self, key_name: str) -> None:
356319
"camel",
357320
)
358321
else:
359-
kebab_name, from_case = key_name, None
322+
# kebab case means the raw name from the attribute will be passed through
323+
kebab_name, from_case = key_name, "kebab"
360324
self._key = kebab_name
361325
if from_case:
362326
self._mods["case"] = [from_case]
@@ -393,43 +357,43 @@ def debounce(
393357
wait: int | str,
394358
*,
395359
leading: bool = False,
396-
notrail: bool = False,
360+
notrailing: bool = False,
397361
) -> Self:
398362
"""Debounce the event listener.
399363
400364
:param wait: The minimum interval between events.
401365
:param leading: If True, the event listener will be called on the leading edge of the
402366
wait time.
403-
:param notrail: If True, the event listener will not be called on the trailing edge of the
367+
:param notrailing: If True, the event listener will not be called on the trailing edge of the
404368
wait time.
405369
"""
406370
self._mods["debounce"] = [str(wait)]
407371
if leading:
408372
self._mods["debounce"].append("leading")
409-
if notrail:
410-
self._mods["debounce"].append("notrail")
373+
if notrailing:
374+
self._mods["debounce"].append("notrailing")
411375
return self
412376

413377
def throttle(
414378
self: Self,
415379
wait: int | str,
416380
*,
417381
noleading: bool = False,
418-
trail: bool = False,
382+
trailing: bool = False,
419383
) -> Self:
420384
"""Throttle the event listener.
421385
422386
:param wait: The minimum interval between events.
423387
:param noleading: If true, the event listener will not be called on the leading edge of the
424388
wait time.
425-
:param trail: If true, the event listener will be called on the trailing edge of the
389+
:param trailing: If true, the event listener will be called on the trailing edge of the
426390
wait time.
427391
"""
428392
self._mods["throttle"] = [str(wait)]
429393
if noleading:
430394
self._mods["throttle"].append("noleading")
431-
if trail:
432-
self._mods["throttle"].append("trail")
395+
if trailing:
396+
self._mods["throttle"].append("trailing")
433397
return self
434398

435399

@@ -672,8 +636,8 @@ def duration(self, duration: int | float | str, *, leading: bool = False) -> Sel
672636
return self
673637

674638

675-
class OnLoadAttr(BaseAttr, ViewtransitionMod, DelayMod):
676-
_attr = "on-load"
639+
class InitAttr(BaseAttr, ViewtransitionMod, DelayMod):
640+
_attr = "init"
677641

678642
@property
679643
def once(self) -> Self:

0 commit comments

Comments
 (0)