text.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """Components for rendering text.
  2. https://www.radix-ui.com/themes/docs/theme/typography
  3. """
  4. from __future__ import annotations
  5. from typing import Literal
  6. from reflex.components.component import ComponentNamespace
  7. from reflex.components.core.breakpoints import Responsive
  8. from reflex.components.el import elements
  9. from reflex.components.markdown.markdown import MarkdownComponentMap
  10. from reflex.vars.base import Var
  11. from ..base import LiteralAccentColor, RadixThemesComponent
  12. from .base import LiteralTextAlign, LiteralTextSize, LiteralTextTrim, LiteralTextWeight
  13. LiteralType = Literal[
  14. "p",
  15. "label",
  16. "div",
  17. "span",
  18. "b",
  19. "i",
  20. "u",
  21. "abbr",
  22. "cite",
  23. "del",
  24. "em",
  25. "ins",
  26. "kbd",
  27. "mark",
  28. "s",
  29. "samp",
  30. "sub",
  31. "sup",
  32. ]
  33. class Text(elements.Span, RadixThemesComponent, MarkdownComponentMap):
  34. """A foundational text primitive based on the <span> element."""
  35. tag = "Text"
  36. # Change the default rendered element for the one passed as a child, merging their props and behavior.
  37. as_child: Var[bool]
  38. # Change the default rendered element into a semantically appropriate alternative (cannot be used with asChild)
  39. as_: Var[LiteralType] = Var.create("p")
  40. # Text size: "1" - "9"
  41. size: Var[Responsive[LiteralTextSize]]
  42. # Thickness of text: "light" | "regular" | "medium" | "bold"
  43. weight: Var[Responsive[LiteralTextWeight]]
  44. # Alignment of text in element: "left" | "center" | "right"
  45. align: Var[Responsive[LiteralTextAlign]]
  46. # Removes the leading trim space: "normal" | "start" | "end" | "both"
  47. trim: Var[Responsive[LiteralTextTrim]]
  48. # Overrides the accent color inherited from the Theme.
  49. color_scheme: Var[LiteralAccentColor]
  50. # Whether to render the text with higher contrast color
  51. high_contrast: Var[bool]
  52. class Span(Text):
  53. """A variant of text rendering as <span> element."""
  54. as_: Var[LiteralType] = Var.create("span")
  55. class Em(elements.Em, RadixThemesComponent):
  56. """Marks text to stress emphasis."""
  57. tag = "Em"
  58. class Kbd(elements.Kbd, RadixThemesComponent):
  59. """Represents keyboard input or a hotkey."""
  60. tag = "Kbd"
  61. # Text size: "1" - "9"
  62. size: Var[LiteralTextSize]
  63. class Quote(elements.Q, RadixThemesComponent):
  64. """A short inline quotation."""
  65. tag = "Quote"
  66. class Strong(elements.Strong, RadixThemesComponent):
  67. """Marks text to signify strong importance."""
  68. tag = "Strong"
  69. class TextNamespace(ComponentNamespace):
  70. """Checkbox components namespace."""
  71. __call__ = staticmethod(Text.create)
  72. em = staticmethod(Em.create)
  73. kbd = staticmethod(Kbd.create)
  74. quote = staticmethod(Quote.create)
  75. strong = staticmethod(Strong.create)
  76. span = staticmethod(Span.create)
  77. text = TextNamespace()