Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/datastar_py/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def attr(self, attr_dict: Mapping | None = None, /, **attrs: str) -> BaseAttr:

def bind(self, signal_name: str) -> BaseAttr:
"""Set up two-way data binding between a signal and an element's value."""
return BaseAttr("bind", value=signal_name, alias=self._alias)
return BindAttr(value=signal_name, alias=self._alias)

def class_(self, class_dict: Mapping | None = None, /, **classes: str) -> BaseAttr:
"""Add or removes classes to or from an element based on expressions."""
Expand Down Expand Up @@ -432,6 +432,21 @@ def self(self) -> Self:
return self


class BindAttr(BaseAttr):
_attr = "bind"

def prop(self, prop: str) -> Self:
"""Bind to a specified property."""
self._mods["prop"] = [prop]
return self

def event(self, event: str | Iterable[str]) -> Self:
"""Only update the signal when the specified events are fired."""
events = [event] if isinstance(event, str) else list(event)
self._mods["event"] = events
return self


class OnAttr(BaseAttr, TimingMod, DelayMod, ViewtransitionMod):
_attr = "on"

Expand Down Expand Up @@ -459,6 +474,12 @@ def window(self) -> Self:
self._mods["window"] = []
return self

@property
def document(self) -> Self:
"""Attach the event listener to the document element."""
self._mods["document"] = []
return self

@property
def outside(self) -> Self:
"""Trigger when the event is outside the element."""
Expand Down
Loading