image.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """Image component from next/image."""
  2. from typing import Any, Literal, Optional, Union
  3. from reflex.event import EventHandler, no_args_event_spec
  4. from reflex.utils import types
  5. from reflex.vars.base import Var
  6. from .base import NextComponent
  7. class Image(NextComponent):
  8. """Display an image."""
  9. tag = "Image"
  10. library = "next/image"
  11. is_default = True
  12. # This can be either an absolute external URL, or an internal path
  13. src: Var[Any]
  14. # Represents the rendered width in pixels, so it will affect how large the image appears.
  15. width: Var[Any]
  16. # Represents the rendered height in pixels, so it will affect how large the image appears.
  17. height: Var[Any]
  18. # Used to describe the image for screen readers and search engines.
  19. alt: Var[str]
  20. # A custom function used to resolve image URLs.
  21. loader: Var[Any]
  22. # A boolean that causes the image to fill the parent element, which is useful when the width and height are unknown. Default to True
  23. fill: Var[bool]
  24. # A string, similar to a media query, that provides information about how wide the image will be at different breakpoints.
  25. sizes: Var[str]
  26. # The quality of the optimized image, an integer between 1 and 100, where 100 is the best quality and therefore largest file size. Defaults to 75.
  27. quality: Var[int]
  28. # When true, the image will be considered high priority and preload. Lazy loading is automatically disabled for images using priority.
  29. priority: Var[bool]
  30. # A placeholder to use while the image is loading. Possible values are blur, empty, or data:image/.... Defaults to empty.
  31. placeholder: Var[str]
  32. # Allows passing CSS styles to the underlying image element.
  33. # style: Var[Any]
  34. # The loading behavior of the image. Defaults to lazy. Can hurt performance, recommended to use `priority` instead.
  35. loading: Var[Literal["lazy", "eager"]]
  36. # A Data URL to be used as a placeholder image before the src image successfully loads. Only takes effect when combined with placeholder="blur".
  37. blurDataURL: Var[str]
  38. # Fires when the image has loaded.
  39. on_load: EventHandler[no_args_event_spec]
  40. # Fires when the image has an error.
  41. on_error: EventHandler[no_args_event_spec]
  42. @classmethod
  43. def create(
  44. cls,
  45. *children,
  46. width: Optional[Union[int, str]] = None,
  47. height: Optional[Union[int, str]] = None,
  48. **props,
  49. ):
  50. """Create an Image component from next/image.
  51. Args:
  52. *children: The children of the component.
  53. width: The width of the image.
  54. height: The height of the image.
  55. **props:The props of the component.
  56. Returns:
  57. _type_: _description_
  58. """
  59. style = props.get("style", {})
  60. DEFAULT_W_H = "100%"
  61. def check_prop_type(prop_name, prop_value):
  62. if types.check_prop_in_allowed_types(prop_value, allowed_types=[int]):
  63. props[prop_name] = prop_value
  64. elif types.check_prop_in_allowed_types(prop_value, allowed_types=[str]):
  65. props[prop_name] = 0
  66. style[prop_name] = prop_value
  67. else:
  68. props[prop_name] = 0
  69. style[prop_name] = DEFAULT_W_H
  70. check_prop_type("width", width)
  71. check_prop_type("height", height)
  72. props["style"] = style
  73. # mysteriously, following `sizes` prop is needed to avoid blury images.
  74. props["sizes"] = "100vw"
  75. return super().create(*children, **props)