media.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. """Media classes."""
  2. from typing import Any, Literal
  3. from reflex import Component, ComponentNamespace
  4. from reflex.components.el.elements.inline import ReferrerPolicy
  5. from reflex.constants.colors import Color
  6. from reflex.vars.base import Var
  7. from .base import BaseHTML
  8. class Area(BaseHTML):
  9. """Display the area element."""
  10. tag = "area"
  11. # Alternate text for the area, used for accessibility
  12. alt: Var[str]
  13. # Coordinates to define the shape of the area
  14. coords: Var[str]
  15. # Specifies that the target will be downloaded when clicked
  16. download: Var[str | bool]
  17. # Hyperlink reference for the area
  18. href: Var[str]
  19. # Language of the linked resource
  20. href_lang: Var[str]
  21. # Specifies what media/device the linked resource is optimized for
  22. media: Var[str]
  23. # Specifies which referrer information to send with the link
  24. referrer_policy: Var[ReferrerPolicy]
  25. # Specifies the relationship of the target object to the link object
  26. rel: Var[str]
  27. # Defines the shape of the area (rectangle, circle, polygon)
  28. shape: Var[str]
  29. # Specifies where to open the linked document
  30. target: Var[str]
  31. CrossOrigin = Literal["anonymous", "use-credentials", ""]
  32. class Audio(BaseHTML):
  33. """Display the audio element."""
  34. tag = "audio"
  35. # Specifies that the audio will start playing as soon as it is ready
  36. auto_play: Var[bool]
  37. # Displays the standard audio controls
  38. controls: Var[bool]
  39. # Configures the CORS requests for the element
  40. cross_origin: Var[CrossOrigin]
  41. # Specifies that the audio will loop
  42. loop: Var[bool]
  43. # Indicates whether the audio is muted by default
  44. muted: Var[bool]
  45. # Specifies how the audio file should be preloaded
  46. preload: Var[str]
  47. # URL of the audio to play
  48. src: Var[str]
  49. ImageDecoding = Literal["async", "auto", "sync"]
  50. ImageLoading = Literal["eager", "lazy"]
  51. class Img(BaseHTML):
  52. """Display the img element."""
  53. tag = "img"
  54. # Alternative text for the image
  55. alt: Var[str]
  56. # Configures the CORS requests for the image
  57. cross_origin: Var[CrossOrigin]
  58. # How the image should be decoded
  59. decoding: Var[ImageDecoding]
  60. # Specifies the loading behavior of the image
  61. loading: Var[ImageLoading]
  62. # Referrer policy for the image
  63. referrer_policy: Var[ReferrerPolicy]
  64. # Sizes of the image for different layouts
  65. sizes: Var[str]
  66. # URL of the image to display
  67. src: Var[Any]
  68. # A set of source sizes and URLs for responsive images
  69. src_set: Var[str]
  70. # The name of the map to use with the image
  71. use_map: Var[str]
  72. @classmethod
  73. def create(cls, *children, **props) -> Component:
  74. """Override create method to apply source attribute to value if user fails to pass in attribute.
  75. Args:
  76. *children: The children of the component.
  77. **props: The props of the component.
  78. Returns:
  79. The component.
  80. """
  81. return (
  82. super().create(src=children[0], **props)
  83. if children
  84. else super().create(*children, **props)
  85. )
  86. class Map(BaseHTML):
  87. """Display the map element."""
  88. tag = "map"
  89. # Name of the map, referenced by the 'usemap' attribute in 'img' and 'object' elements
  90. name: Var[str]
  91. class Track(BaseHTML):
  92. """Display the track element."""
  93. tag = "track"
  94. # Indicates that the track should be enabled unless the user's preferences indicate otherwise
  95. default: Var[bool]
  96. # Specifies the kind of text track
  97. kind: Var[str]
  98. # Title of the text track, used by the browser when listing available text tracks
  99. label: Var[str]
  100. # URL of the track file
  101. src: Var[str]
  102. # Language of the track text data
  103. src_lang: Var[str]
  104. class Video(BaseHTML):
  105. """Display the video element."""
  106. tag = "video"
  107. # Specifies that the video will start playing as soon as it is ready
  108. auto_play: Var[bool]
  109. # Displays the standard video controls
  110. controls: Var[bool]
  111. # Configures the CORS requests for the video
  112. cross_origin: Var[CrossOrigin]
  113. # Specifies that the video will loop
  114. loop: Var[bool]
  115. # Indicates whether the video is muted by default
  116. muted: Var[bool]
  117. # Indicates that the video should play 'inline', inside its element's playback area
  118. plays_inline: Var[bool]
  119. # URL of an image to show while the video is downloading, or until the user hits the play button
  120. poster: Var[str]
  121. # Specifies how the video file should be preloaded
  122. preload: Var[str]
  123. # URL of the video to play
  124. src: Var[str]
  125. class Embed(BaseHTML):
  126. """Display the embed element."""
  127. tag = "embed"
  128. # URL of the embedded content
  129. src: Var[str]
  130. # Media type of the embedded content
  131. type: Var[str]
  132. class Iframe(BaseHTML):
  133. """Display the iframe element."""
  134. tag = "iframe"
  135. # Permissions policy for the iframe
  136. allow: Var[str]
  137. # Specifies the loading behavior of the iframe
  138. loading: Var[Literal["eager", "lazy"]]
  139. # Name of the iframe, used as a target for hyperlinks and forms
  140. name: Var[str]
  141. # Referrer policy for the iframe
  142. referrer_policy: Var[ReferrerPolicy]
  143. # Security restrictions for the content in the iframe
  144. sandbox: Var[str]
  145. # URL of the document to display in the iframe
  146. src: Var[str]
  147. # HTML content to embed directly within the iframe
  148. src_doc: Var[str]
  149. class Object(BaseHTML):
  150. """Display the object element."""
  151. tag = "object"
  152. # URL of the data to be used by the object
  153. data: Var[str]
  154. # Associates the object with a form element
  155. form: Var[str]
  156. # Name of the object, used for scripting or as a target for forms and links
  157. name: Var[str]
  158. # Media type of the data specified in the data attribute
  159. type: Var[str]
  160. # Name of an image map to use with the object
  161. use_map: Var[str]
  162. class Picture(BaseHTML):
  163. """Display the picture element."""
  164. tag = "picture"
  165. class Portal(BaseHTML):
  166. """Display the portal element."""
  167. tag = "portal"
  168. class Source(BaseHTML):
  169. """Display the source element."""
  170. tag = "source"
  171. # Media query indicating what device the linked resource is optimized for
  172. media: Var[str]
  173. # Sizes of the source for different layouts
  174. sizes: Var[str]
  175. # URL of the media file or an image for the element to use
  176. src: Var[str]
  177. # A set of source sizes and URLs for responsive images
  178. src_set: Var[str]
  179. # Media type of the source
  180. type: Var[str]
  181. class Svg(BaseHTML):
  182. """Display the svg element."""
  183. tag = "svg"
  184. # The width of the svg.
  185. width: Var[str | int]
  186. # The height of the svg.
  187. height: Var[str | int]
  188. # The XML namespace declaration.
  189. xmlns: Var[str]
  190. class Text(BaseHTML):
  191. """The SVG text component."""
  192. tag = "text"
  193. # The x coordinate of the starting point of the text baseline.
  194. x: Var[str | int]
  195. # The y coordinate of the starting point of the text baseline.
  196. y: Var[str | int]
  197. # Shifts the text position horizontally from a previous text element.
  198. dx: Var[str | int]
  199. # Shifts the text position vertically from a previous text element.
  200. dy: Var[str | int]
  201. # Rotates orientation of each individual glyph.
  202. rotate: Var[str | int]
  203. # How the text is stretched or compressed to fit the width defined by the text_length attribute.
  204. length_adjust: Var[str]
  205. # A width that the text should be scaled to fit.
  206. text_length: Var[str | int]
  207. class Line(BaseHTML):
  208. """The SVG line component."""
  209. tag = "line"
  210. # The x-axis coordinate of the line starting point.
  211. x1: Var[str | int]
  212. # The x-axis coordinate of the the line ending point.
  213. x2: Var[str | int]
  214. # The y-axis coordinate of the line starting point.
  215. y1: Var[str | int]
  216. # The y-axis coordinate of the the line ending point.
  217. y2: Var[str | int]
  218. # The total path length, in user units.
  219. path_length: Var[int]
  220. class Circle(BaseHTML):
  221. """The SVG circle component."""
  222. tag = "circle"
  223. # The x-axis coordinate of the center of the circle.
  224. cx: Var[str | int]
  225. # The y-axis coordinate of the center of the circle.
  226. cy: Var[str | int]
  227. # The radius of the circle.
  228. r: Var[str | int]
  229. # The total length for the circle's circumference, in user units.
  230. path_length: Var[int]
  231. class Ellipse(BaseHTML):
  232. """The SVG ellipse component."""
  233. tag = "ellipse"
  234. # The x position of the center of the ellipse.
  235. cx: Var[str | int]
  236. # The y position of the center of the ellipse.
  237. cy: Var[str | int]
  238. # The radius of the ellipse on the x axis.
  239. rx: Var[str | int]
  240. # The radius of the ellipse on the y axis.
  241. ry: Var[str | int]
  242. # The total length for the ellipse's circumference, in user units.
  243. path_length: Var[int]
  244. class Rect(BaseHTML):
  245. """The SVG rect component."""
  246. tag = "rect"
  247. # The x coordinate of the rect.
  248. x: Var[str | int]
  249. # The y coordinate of the rect.
  250. y: Var[str | int]
  251. # The width of the rect
  252. width: Var[str | int]
  253. # The height of the rect.
  254. height: Var[str | int]
  255. # The horizontal corner radius of the rect. Defaults to ry if it is specified.
  256. rx: Var[str | int]
  257. # The vertical corner radius of the rect. Defaults to rx if it is specified.
  258. ry: Var[str | int]
  259. # The total length of the rectangle's perimeter, in user units.
  260. path_length: Var[int]
  261. class Polygon(BaseHTML):
  262. """The SVG polygon component."""
  263. tag = "polygon"
  264. # defines the list of points (pairs of x,y absolute coordinates) required to draw the polygon.
  265. points: Var[str]
  266. # This prop lets specify the total length for the path, in user units.
  267. path_length: Var[int]
  268. class Defs(BaseHTML):
  269. """Display the defs element."""
  270. tag = "defs"
  271. class LinearGradient(BaseHTML):
  272. """Display the linearGradient element."""
  273. tag = "linearGradient"
  274. # Units for the gradient.
  275. gradient_units: Var[str | bool]
  276. # Transform applied to the gradient.
  277. gradient_transform: Var[str | bool]
  278. # Method used to spread the gradient.
  279. spread_method: Var[str | bool]
  280. # X coordinate of the starting point of the gradient.
  281. x1: Var[str | int | float]
  282. # X coordinate of the ending point of the gradient.
  283. x2: Var[str | int | float]
  284. # Y coordinate of the starting point of the gradient.
  285. y1: Var[str | int | float]
  286. # Y coordinate of the ending point of the gradient.
  287. y2: Var[str | int | float]
  288. class RadialGradient(BaseHTML):
  289. """Display the radialGradient element."""
  290. tag = "radialGradient"
  291. # The x coordinate of the end circle of the radial gradient.
  292. cx: Var[str | int | float]
  293. # The y coordinate of the end circle of the radial gradient.
  294. cy: Var[str | int | float]
  295. # The radius of the start circle of the radial gradient.
  296. fr: Var[str | int | float]
  297. # The x coordinate of the start circle of the radial gradient.
  298. fx: Var[str | int | float]
  299. # The y coordinate of the start circle of the radial gradient.
  300. fy: Var[str | int | float]
  301. # Units for the gradient.
  302. gradient_units: Var[str | bool]
  303. # Transform applied to the gradient.
  304. gradient_transform: Var[str | bool]
  305. # The radius of the end circle of the radial gradient.
  306. r: Var[str | int | float]
  307. # Method used to spread the gradient.
  308. spread_method: Var[str | bool]
  309. class Stop(BaseHTML):
  310. """Display the stop element."""
  311. tag = "stop"
  312. # Offset of the gradient stop.
  313. offset: Var[str | float | int]
  314. # Color of the gradient stop.
  315. stop_color: Var[str | Color | bool]
  316. # Opacity of the gradient stop.
  317. stop_opacity: Var[str | float | int | bool]
  318. class Path(BaseHTML):
  319. """Display the path element."""
  320. tag = "path"
  321. # Defines the shape of the path.
  322. d: Var[str | int | float]
  323. class SVG(ComponentNamespace):
  324. """SVG component namespace."""
  325. text = staticmethod(Text.create)
  326. line = staticmethod(Line.create)
  327. circle = staticmethod(Circle.create)
  328. ellipse = staticmethod(Ellipse.create)
  329. rect = staticmethod(Rect.create)
  330. polygon = staticmethod(Polygon.create)
  331. path = staticmethod(Path.create)
  332. stop = staticmethod(Stop.create)
  333. linear_gradient = staticmethod(LinearGradient.create)
  334. radial_gradient = staticmethod(RadialGradient.create)
  335. defs = staticmethod(Defs.create)
  336. __call__ = staticmethod(Svg.create)
  337. text = Text.create
  338. line = Line.create
  339. circle = Circle.create
  340. ellipse = Ellipse.create
  341. rect = Rect.create
  342. polygon = Polygon.create
  343. path = Path.create
  344. stop = Stop.create
  345. linear_gradient = LinearGradient.create
  346. radial_gradient = RadialGradient.create
  347. defs = Defs.create
  348. area = Area.create
  349. audio = Audio.create
  350. image = img = Img.create
  351. map = Map.create
  352. track = Track.create
  353. video = Video.create
  354. embed = Embed.create
  355. iframe = Iframe.create
  356. object = Object.create
  357. picture = Picture.create
  358. portal = Portal.create
  359. source = Source.create
  360. svg = SVG()