progress.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Progress."""
  2. from typing import Optional
  3. from reflex.components.component import Component
  4. from reflex.components.radix.primitives.base import RadixPrimitiveComponent
  5. from reflex.style import Style
  6. from reflex.vars import Var
  7. class ProgressComponent(RadixPrimitiveComponent):
  8. """A Progress component."""
  9. library = "@radix-ui/react-progress@^1.0.3"
  10. class ProgressRoot(ProgressComponent):
  11. """The Progress Root component."""
  12. tag = "Root"
  13. alias = "RadixProgressRoot"
  14. # The current progress value.
  15. value: Var[Optional[int]]
  16. # The maximum progress value.
  17. max: Var[int]
  18. def _apply_theme(self, theme: Component | None):
  19. self.style = Style(
  20. {
  21. "position": "relative",
  22. "overflow": "hidden",
  23. "background": "black",
  24. "border_radius": "99999px",
  25. "width": "300px",
  26. "height": "25px",
  27. **self.style,
  28. }
  29. )
  30. class ProgressIndicator(ProgressComponent):
  31. """The Progress bar indicator."""
  32. tag = "Indicator"
  33. alias = "RadixProgressIndicator"
  34. # The current progress value.
  35. value: Var[Optional[int]]
  36. def _apply_theme(self, theme: Component | None):
  37. self.style = Style(
  38. {
  39. "background-color": "white",
  40. "width": "100%",
  41. "height": "100%",
  42. "transition": f"transform 660ms linear",
  43. "&[data_state='loading']": {
  44. "transition": f"transform 660ms linear",
  45. },
  46. "transform": f"translateX(-{100 - self.value}%)", # type: ignore
  47. }
  48. )
  49. progress_root = ProgressRoot.create
  50. progress_indicator = ProgressIndicator.create
  51. def progress(**props):
  52. """High level API for progress bar.
  53. Args:
  54. **props: The props of the progress bar
  55. Returns:
  56. The progress bar.
  57. """
  58. return progress_root(
  59. progress_indicator(value=props.get("value")),
  60. **props,
  61. )