mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Merge pull request #918 from davep/css-docs-review-reactive
CSS docs review - Reactivity
This commit is contained in:
@@ -35,7 +35,7 @@ The `reactive` constructor accepts a default value as the first positional argum
|
||||
|
||||
Textual uses Python's _descriptor protocol_ to create reactive attributes, which is the same protocol used by the builtin `property` decorator.
|
||||
|
||||
You can get and set these attributes in the same way as if you had assigned them in a `__init__` method. For instance `self.name = "Jessica"`, `self.count += 1`, or `print(self.is_cool)`.
|
||||
You can get and set these attributes in the same way as if you had assigned them in an `__init__` method. For instance `self.name = "Jessica"`, `self.count += 1`, or `print(self.is_cool)`.
|
||||
|
||||
### Dynamic defaults
|
||||
|
||||
@@ -69,7 +69,7 @@ The first superpower we will look at is "smart refresh". When you modify a react
|
||||
|
||||
!!! information
|
||||
|
||||
If you modify multiple reactive attribute, Textual will only do a single refresh to minimize updates.
|
||||
If you modify multiple reactive attributes, Textual will only do a single refresh to minimize updates.
|
||||
|
||||
Let's look at an example which illustrates this. In the following app, the value of an input is used to update a "Hello, World!" type greeting.
|
||||
|
||||
@@ -167,7 +167,7 @@ If you click the buttons in the above example it will show the current count. Wh
|
||||
|
||||
Watch methods are another superpower. Textual will call watch methods when reactive attributes are modified. Watch methods begin with `watch_` followed by the name of the attribute. If the watch method accepts a positional argument, it will be called with the new assigned value. If the watch method accepts *two* positional arguments, it will be called with both the *old* value and the *new* value.
|
||||
|
||||
The follow app will display any color you type in to the input. Try it with a valid color in Textual CSS. For example `"darkorchid"` or `"#52de44".
|
||||
The following app will display any color you type in to the input. Try it with a valid color in Textual CSS. For example `"darkorchid"` or `"#52de44"`.
|
||||
|
||||
=== "watch01.py"
|
||||
|
||||
@@ -198,7 +198,7 @@ Compute methods are the final superpower offered by the `reactive` descriptor. T
|
||||
|
||||
You could be forgiven in thinking this sounds a lot like Python's property decorator. The difference is that Textual will cache the value of compute methods, and update them when any other reactive attribute changes.
|
||||
|
||||
The following example uses a computed attribute. It displays three inputs for the each color component (red, green, and blue). If you enter numbers in to these inputs, the background color of another widget changes.
|
||||
The following example uses a computed attribute. It displays three inputs for each color component (red, green, and blue). If you enter numbers in to these inputs, the background color of another widget changes.
|
||||
|
||||
=== "computed01.py"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user