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