Base toggle button widget. Warning: `ToggleButton` should be considered to be an internal class; it exists to serve as the common core of [Checkbox][textual.widgets.Checkbox] and [RadioButton][textual.widgets.RadioButton].
| 23 | |
| 24 | |
| 25 | class ToggleButton(Static, can_focus=True): |
| 26 | """Base toggle button widget. |
| 27 | |
| 28 | Warning: |
| 29 | `ToggleButton` should be considered to be an internal class; it |
| 30 | exists to serve as the common core of [Checkbox][textual.widgets.Checkbox] and |
| 31 | [RadioButton][textual.widgets.RadioButton]. |
| 32 | """ |
| 33 | |
| 34 | ALLOW_SELECT = False |
| 35 | BINDINGS: ClassVar[list[BindingType]] = [ |
| 36 | Binding("enter,space", "toggle_button", "Toggle", show=False), |
| 37 | ] |
| 38 | """ |
| 39 | | Key(s) | Description | |
| 40 | | :- | :- | |
| 41 | | enter, space | Toggle the value. | |
| 42 | """ |
| 43 | |
| 44 | COMPONENT_CLASSES: ClassVar[set[str]] = { |
| 45 | "toggle--button", |
| 46 | "toggle--label", |
| 47 | } |
| 48 | """ |
| 49 | | Class | Description | |
| 50 | | :- | :- | |
| 51 | | `toggle--button` | Targets the toggle button itself. | |
| 52 | | `toggle--label` | Targets the text label of the toggle button. | |
| 53 | """ |
| 54 | |
| 55 | DEFAULT_CSS = """ |
| 56 | ToggleButton { |
| 57 | width: auto; |
| 58 | border: tall $border-blurred; |
| 59 | padding: 0 1; |
| 60 | background: $surface; |
| 61 | text-wrap: nowrap; |
| 62 | text-overflow: ellipsis; |
| 63 | pointer: pointer; |
| 64 | |
| 65 | &:ansi { |
| 66 | background: ansi_default; |
| 67 | & > .toggle--button { |
| 68 | background: $ansi-background; |
| 69 | color: $ansi-foreground; |
| 70 | text-style: dim; |
| 71 | } |
| 72 | &.-on > .toggle--button { |
| 73 | color: $accent; |
| 74 | background: $ansi-background; |
| 75 | text-style: bold not dim; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | &.-textual-compact { |
| 80 | border: none !important; |
| 81 | padding: 0; |
| 82 | &:focus { |