code.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """A code component."""
  2. from typing import Dict
  3. from reflex.components.component import Component
  4. from reflex.components.forms import Button
  5. from reflex.components.layout import Box
  6. from reflex.components.libs.chakra import ChakraComponent
  7. from reflex.components.media import Icon
  8. from reflex.event import set_clipboard
  9. from reflex.style import Style
  10. from reflex.utils import imports
  11. from reflex.vars import ImportVar, Var
  12. # Path to the prism styles.
  13. PRISM_STYLES_PATH = "/styles/code/prism"
  14. class CodeBlock(Component):
  15. """A code block."""
  16. library = "react-syntax-highlighter"
  17. tag = "Prism"
  18. # The theme to use ("light" or "dark").
  19. theme: Var[str]
  20. # The language to use.
  21. language: Var[str]
  22. # If this is enabled line numbers will be shown next to the code block.
  23. show_line_numbers: Var[bool]
  24. # The starting line number to use.
  25. starting_line_number: Var[int]
  26. # Whether to wrap long lines.
  27. wrap_long_lines: Var[bool]
  28. # A custom style for the code block.
  29. custom_style: Var[Dict[str, str]]
  30. # Props passed down to the code tag.
  31. code_tag_props: Var[Dict[str, str]]
  32. def _get_imports(self) -> imports.ImportDict:
  33. merged_imports = super()._get_imports()
  34. if self.theme is not None:
  35. merged_imports = imports.merge_imports(
  36. merged_imports, {PRISM_STYLES_PATH: {ImportVar(tag=self.theme.name)}}
  37. )
  38. return merged_imports
  39. @classmethod
  40. def create(cls, *children, can_copy=False, copy_button=None, **props):
  41. """Create a text component.
  42. Args:
  43. *children: The children of the component.
  44. can_copy: Whether a copy button should appears.
  45. copy_button: A custom copy button to override the default one.
  46. **props: The props to pass to the component.
  47. Returns:
  48. The text component.
  49. """
  50. # This component handles style in a special prop.
  51. custom_style = props.pop("custom_style", {})
  52. if can_copy:
  53. code = children[0]
  54. copy_button = (
  55. copy_button
  56. if copy_button is not None
  57. else Button.create(
  58. Icon.create(tag="copy"),
  59. on_click=set_clipboard(code),
  60. style={"position": "absolute", "top": "0.5em", "right": "0"},
  61. )
  62. )
  63. custom_style.update({"padding": "1em 3.2em 1em 1em"})
  64. else:
  65. copy_button = None
  66. # Transfer style props to the custom style prop.
  67. for key, value in props.items():
  68. if key not in cls.get_fields():
  69. custom_style[key] = value
  70. # Create the component.
  71. code_block = super().create(
  72. *children,
  73. **props,
  74. custom_style=Style(custom_style),
  75. )
  76. if copy_button:
  77. return Box.create(code_block, copy_button, position="relative")
  78. else:
  79. return code_block
  80. def _add_style(self, style):
  81. self.custom_style = self.custom_style or {}
  82. self.custom_style.update(style) # type: ignore
  83. def _render(self):
  84. out = super()._render()
  85. if self.theme is not None:
  86. out.add_props(
  87. style=Var.create(self.theme.name, is_local=False)
  88. ).remove_props("theme")
  89. return out
  90. class Code(ChakraComponent):
  91. """Used to display inline code."""
  92. tag = "Code"