one
Version:
One is a new React Framework that makes Vite serve both native and web.
95 lines (94 loc) • 2.12 kB
JavaScript
import { Slot } from "@radix-ui/react-slot";
import * as React from "react";
import { Platform, Text } from "react-native-web";
import { resolveHref } from "./href.mjs";
import { useLinkTo } from "./useLinkTo.mjs";
import { jsx } from "react/jsx-runtime";
const Link = React.forwardRef(function Link2({
href,
replace,
push,
id,
mask,
asChild,
rel,
target,
download,
...rest
}, ref) {
const style = useInteropClassName(rest);
const hrefAttrs = useHrefAttrs({
asChild,
rel,
target,
download
});
const props = useLinkTo({
href: React.useMemo(() => {
if (href == null) throw new Error("Link: href is required");
return resolveHref(href);
}, [href]),
replace,
mask: React.useMemo(() => {
return mask ? resolveHref(mask) : void 0;
}, [mask])
});
const onPress = e => {
if ("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) {
if (Platform.OS !== "web") return props.style;
return React.useMemo(() => {
if (props.className == null) return props.style;
const cssStyle = {
$$css: true,
__routerLinkClassName: props.className
};
if (Array.isArray(props.style)) return [...props.style, cssStyle];
return [props.style, cssStyle];
}, [props.style, props.className]);
}
const useHrefAttrs = Platform.select({
web: function useHrefAttrs2({
asChild,
rel,
target,
download
}) {
return React.useMemo(() => {
const hrefAttrs = {
rel,
target,
download
};
if (asChild) return hrefAttrs;
return {
hrefAttrs
};
}, [asChild, rel, target, download]);
},
default: function useHrefAttrs3() {
return {};
}
});
export { Link };
//# sourceMappingURL=Link.mjs.map