UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

83 lines (82 loc) 2.11 kB
import { Slot } from "@radix-ui/react-slot"; import * as React from "react"; import { Platform, Text } from "react-native-web"; import { resolveHref } from "./href"; import { useLinkTo } from "./useLinkTo"; import { jsx } from "react/jsx-runtime"; const Link = React.forwardRef(function({ href, replace, push, id, mask, // TODO: This does not prevent default on the anchor tag. asChild, rel, target, download, ...rest }, ref) { const style = useInteropClassName(rest), hrefAttrs = useHrefAttrs({ asChild, rel, target, download }), resolvedHref = React.useMemo(() => { if (href == null) throw new Error("Link: href is required"); return resolveHref(href); }, [href]), resolvedMask = React.useMemo(() => mask ? resolveHref(mask) : void 0, [mask]), props = useLinkTo({ href: resolvedHref, replace, mask: resolvedMask }), onPress = (e) => { "onPress" in rest && rest.onPress?.(e), props.onPress(e); }; return /* @__PURE__ */ jsx( asChild ? Slot : Text, { ref, ...props, ...hrefAttrs, ...rest, id, style: asChild ? null : style, ...Platform.select({ web: { onClick: onPress }, default: { onPress } }) } ); }); Link.resolveHref = resolveHref; function useInteropClassName(props) { return Platform.OS !== "web" ? props.style : React.useMemo(() => { if (props.className == null) return props.style; const cssStyle = { $$css: !0, __routerLinkClassName: props.className }; return Array.isArray(props.style) ? [...props.style, cssStyle] : [props.style, cssStyle]; }, [props.style, props.className]); } const useHrefAttrs = Platform.select({ web: function({ asChild, rel, target, download }) { return React.useMemo(() => { const hrefAttrs = { rel, target, download }; return asChild ? hrefAttrs : { hrefAttrs }; }, [asChild, rel, target, download]); }, default: function() { return {}; } }); export { Link }; //# sourceMappingURL=Link.js.map