|
| 1 | +"""Custom link component.""" |
| 2 | + |
| 3 | +from typing import Literal |
| 4 | + |
| 5 | +from reflex.components.react_router.dom import ReactRouterLink |
| 6 | +from reflex.vars import Var |
| 7 | + |
| 8 | +from reflex_ui.components.component import CoreComponent |
| 9 | +from reflex_ui.components.icons.hugeicon import hi |
| 10 | + |
| 11 | +LiteralLinkVariant = Literal["primary", "secondary"] |
| 12 | +LiteralLinkSize = Literal["xs", "sm", "md", "lg", "xl"] |
| 13 | + |
| 14 | + |
| 15 | +class ClassNames: |
| 16 | + """Class names for the link component.""" |
| 17 | + |
| 18 | + ROOT = "font-medium underline-offset-2 hover:underline w-fit group/link relative" |
| 19 | + |
| 20 | + |
| 21 | +LINK_VARIANTS: dict[str, dict[str, str]] = { |
| 22 | + "size": { |
| 23 | + "xs": "text-xs", |
| 24 | + "sm": "text-sm", |
| 25 | + "md": "text-md", |
| 26 | + "lg": "text-lg", |
| 27 | + "xl": "text-xl", |
| 28 | + }, |
| 29 | + "variant": { |
| 30 | + "primary": "text-primary-11", |
| 31 | + "secondary": "text-secondary-11", |
| 32 | + }, |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +class Link(ReactRouterLink, CoreComponent): |
| 37 | + """Link component.""" |
| 38 | + |
| 39 | + # The size of the link. Defaults to "sm". |
| 40 | + size: Var[LiteralLinkSize] |
| 41 | + |
| 42 | + # The variant of the link. Defaults to "secondary". |
| 43 | + variant: Var[LiteralLinkVariant] |
| 44 | + |
| 45 | + # Whether to show the icon. Defaults to False. |
| 46 | + show_icon: Var[bool] |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def create(cls, *children, **props) -> ReactRouterLink: |
| 50 | + """Create the link component.""" |
| 51 | + size = props.pop("size", "sm") |
| 52 | + cls.validate_size(size) |
| 53 | + variant = props.pop("variant", "secondary") |
| 54 | + cls.validate_variant(variant) |
| 55 | + show_icon = props.pop("show_icon", False) |
| 56 | + |
| 57 | + # Apply default styling |
| 58 | + cls.set_class_name( |
| 59 | + f"{ClassNames.ROOT} {LINK_VARIANTS['size'][size]} {LINK_VARIANTS['variant'][variant]}", |
| 60 | + props, |
| 61 | + ) |
| 62 | + |
| 63 | + children = list(children) |
| 64 | + if show_icon: |
| 65 | + children.append( |
| 66 | + hi( |
| 67 | + "LinkSquare02Icon", |
| 68 | + class_name="absolute top-1/2 -translate-y-1/2 right-[-1.375rem] group-hover/link:opacity-100 text-secondary-9 opacity-0", |
| 69 | + ), |
| 70 | + ) |
| 71 | + |
| 72 | + return super().create(*children, **props) |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def validate_variant(variant: LiteralLinkVariant): |
| 76 | + """Validate the link variant.""" |
| 77 | + if variant not in LINK_VARIANTS["variant"]: |
| 78 | + available_variants = ", ".join(LINK_VARIANTS["variant"].keys()) |
| 79 | + message = ( |
| 80 | + f"Invalid variant: {variant}. Available variants: {available_variants}" |
| 81 | + ) |
| 82 | + raise ValueError(message) |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def validate_size(size: LiteralLinkSize): |
| 86 | + """Validate the link size.""" |
| 87 | + if size not in LINK_VARIANTS["size"]: |
| 88 | + available_sizes = ", ".join(LINK_VARIANTS["size"].keys()) |
| 89 | + message = f"Invalid size: {size}. Available sizes: {available_sizes}" |
| 90 | + raise ValueError(message) |
| 91 | + |
| 92 | + def _exclude_props(self) -> list[str]: |
| 93 | + return [ |
| 94 | + *super()._exclude_props(), |
| 95 | + "size", |
| 96 | + "variant", |
| 97 | + "show_icon", |
| 98 | + ] |
| 99 | + |
| 100 | + |
| 101 | +link = Link.create |
0 commit comments