progress.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """Progress."""
  2. from __future__ import annotations
  3. from typing import Optional, Union
  4. from reflex.components.component import Component, ComponentNamespace
  5. from reflex.components.core.colors import color
  6. from reflex.components.radix.primitives.accordion import DEFAULT_ANIMATION_DURATION
  7. from reflex.components.radix.primitives.base import RadixPrimitiveComponentWithClassName
  8. from reflex.components.radix.themes.base import LiteralAccentColor
  9. from reflex.style import Style
  10. from reflex.vars import Var
  11. class ProgressComponent(RadixPrimitiveComponentWithClassName):
  12. """A Progress component."""
  13. library = "@radix-ui/react-progress@^1.0.3"
  14. class ProgressRoot(ProgressComponent):
  15. """The Progress Root component."""
  16. tag = "Root"
  17. alias = "RadixProgressRoot"
  18. # The current progress value.
  19. value: Var[Optional[int]]
  20. # The maximum progress value.
  21. max: Var[int]
  22. def _apply_theme(self, theme: Component):
  23. self.style = Style(
  24. {
  25. "position": "relative",
  26. "overflow": "hidden",
  27. "background": "var(--gray-a3)",
  28. "border_radius": "99999px",
  29. "width": "100%",
  30. "height": "20px",
  31. "boxShadow": "inset 0 0 0 1px var(--gray-a5)",
  32. **self.style,
  33. }
  34. )
  35. class ProgressIndicator(ProgressComponent):
  36. """The Progress bar indicator."""
  37. tag = "Indicator"
  38. alias = "RadixProgressIndicator"
  39. # The current progress value.
  40. value: Var[Optional[int]]
  41. # The maximum progress value.
  42. max: Var[Optional[int]]
  43. # The color scheme of the progress indicator.
  44. color_scheme: Var[LiteralAccentColor]
  45. def _apply_theme(self, theme: Component):
  46. global_color_scheme = getattr(theme, "accent_color", None)
  47. if global_color_scheme is None and self.color_scheme is None:
  48. raise ValueError(
  49. "`color_scheme` cannot be None. Either set the `color_scheme` prop on the progress indicator "
  50. "component or set the `accent_color` prop in your global theme."
  51. )
  52. color_scheme = color(
  53. self.color_scheme if self.color_scheme is not None else global_color_scheme, # type: ignore
  54. 9,
  55. )
  56. self.style = Style(
  57. {
  58. "background-color": color_scheme,
  59. "width": "100%",
  60. "height": "100%",
  61. f"transition": f"transform {DEFAULT_ANIMATION_DURATION}ms linear",
  62. "&[data_state='loading']": {
  63. "transition": f"transform {DEFAULT_ANIMATION_DURATION}ms linear",
  64. },
  65. "transform": f"translateX(calc(-100% + ({self.value} / {self.max} * 100%)))", # type: ignore
  66. "boxShadow": "inset 0 0 0 1px var(--gray-a5)",
  67. }
  68. )
  69. def _exclude_props(self) -> list[str]:
  70. return ["color_scheme"]
  71. class Progress(ComponentNamespace):
  72. """High-level API for progress bar."""
  73. root = staticmethod(ProgressRoot.create)
  74. indicator = staticmethod(ProgressIndicator.create)
  75. @staticmethod
  76. def __call__(
  77. width: Optional[str] = "100%",
  78. color_scheme: Optional[Union[Var, LiteralAccentColor]] = None,
  79. **props,
  80. ) -> Component:
  81. """High-level API for progress bar.
  82. Args:
  83. width: The width of the progress bar.
  84. **props: The props of the progress bar.
  85. color_scheme: The color scheme of the progress indicator.
  86. Returns:
  87. The progress bar.
  88. """
  89. style = props.setdefault("style", {})
  90. style.update({"width": width})
  91. progress_indicator_props = (
  92. {"color_scheme": color_scheme} if color_scheme is not None else {}
  93. )
  94. return ProgressRoot.create(
  95. ProgressIndicator.create(
  96. value=props.get("value"),
  97. max=props.get("max", 100),
  98. **progress_indicator_props, # type: ignore
  99. ),
  100. **props,
  101. )
  102. progress = Progress()