diff --git a/src/datastar_py/attributes.py b/src/datastar_py/attributes.py index 7732a88..7ad7510 100644 --- a/src/datastar_py/attributes.py +++ b/src/datastar_py/attributes.py @@ -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.""" @@ -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" @@ -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."""