tooltip.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """Interactive components provided by @radix-ui/themes."""
  2. from typing import Dict, Literal, Union
  3. from reflex.components.component import Component
  4. from reflex.event import EventHandler, no_args_event_spec, passthrough_event_spec
  5. from reflex.utils import format
  6. from reflex.vars.base import Var
  7. from ..base import (
  8. RadixThemesComponent,
  9. )
  10. LiteralSideType = Literal[
  11. "top",
  12. "right",
  13. "bottom",
  14. "left",
  15. ]
  16. LiteralAlignType = Literal[
  17. "start",
  18. "center",
  19. "end",
  20. ]
  21. LiteralStickyType = Literal[
  22. "partial",
  23. "always",
  24. ]
  25. # The Tooltip inherits props from the Tooltip.Root, Tooltip.Portal, Tooltip.Content
  26. class Tooltip(RadixThemesComponent):
  27. """Floating element that provides a control with contextual information via pointer or focus."""
  28. tag = "Tooltip"
  29. # The content of the tooltip.
  30. content: Var[str]
  31. # The open state of the tooltip when it is initially rendered. Use when you do not need to control its open state.
  32. default_open: Var[bool]
  33. # The controlled open state of the tooltip. Must be used in conjunction with `on_open_change`.
  34. open: Var[bool]
  35. # The preferred side of the trigger to render against when open. Will be reversed when collisions occur and `avoid_collisions` is enabled.The position of the tooltip. Defaults to "top".
  36. side: Var[LiteralSideType]
  37. # The distance in pixels from the trigger. Defaults to 0.
  38. side_offset: Var[Union[float, int]]
  39. # The preferred alignment against the trigger. May change when collisions occur. Defaults to "center".
  40. align: Var[LiteralAlignType]
  41. # An offset in pixels from the "start" or "end" alignment options.
  42. align_offset: Var[Union[float, int]]
  43. # When true, overrides the side and align preferences to prevent collisions with boundary edges. Defaults to True.
  44. avoid_collisions: Var[bool]
  45. # The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { "top": 20, "left": 20 }. Defaults to 0.
  46. collision_padding: Var[Union[float, int, Dict[str, Union[float, int]]]]
  47. # The padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners. Defaults to 0.
  48. arrow_padding: Var[Union[float, int]]
  49. # The sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. Defaults to "partial".
  50. sticky: Var[LiteralStickyType]
  51. # Whether to hide the content when the trigger becomes fully occluded. Defaults to False.
  52. hide_when_detached: Var[bool]
  53. # Override the duration in milliseconds to customize the open delay for a specific tooltip. Default is 700.
  54. delay_duration: Var[Union[float, int]]
  55. # Prevents Tooltip content from remaining open when hovering.
  56. disable_hoverable_content: Var[bool]
  57. # Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.
  58. force_mount: Var[bool]
  59. # By default, screenreaders will announce the content inside the component. If this is not descriptive enough, or you have content that cannot be announced, use aria-label as a more descriptive label.
  60. aria_label: Var[str]
  61. # Fired when the open state changes.
  62. on_open_change: EventHandler[passthrough_event_spec(bool)]
  63. # Fired when the escape key is pressed.
  64. on_escape_key_down: EventHandler[no_args_event_spec]
  65. # Fired when the pointer is down outside the tooltip.
  66. on_pointer_down_outside: EventHandler[no_args_event_spec]
  67. @classmethod
  68. def create(cls, *children, **props) -> Component:
  69. """Initialize the Tooltip component.
  70. Run some additional handling on the props.
  71. Args:
  72. *children: The positional arguments
  73. **props: The keyword arguments
  74. Returns:
  75. The created component.
  76. """
  77. ARIA_LABEL_KEY = "aria_label"
  78. if props.get(ARIA_LABEL_KEY) is not None:
  79. props[format.to_kebab_case(ARIA_LABEL_KEY)] = props.pop(ARIA_LABEL_KEY)
  80. return super().create(*children, **props)
  81. tooltip = Tooltip.create