Support download

This commit is contained in:
Eric Bailey
2024-02-16 12:23:45 -06:00
parent 97b9607c69
commit 18753a853c
+18 -15
View File
@@ -37,6 +37,11 @@ type BaseLinkProps = Pick<
> & { > & {
testID?: string testID?: string
/**
* Label for a11y. Defaults to the href.
*/
label?: string
/** /**
* The React Navigation `StackAction` to perform when the link is pressed. * The React Navigation `StackAction` to perform when the link is pressed.
*/ */
@@ -55,6 +60,11 @@ type BaseLinkProps = Pick<
* DO NOT use this for navigation, that's what the `to` prop is for. * DO NOT use this for navigation, that's what the `to` prop is for.
*/ */
onPress?: (e: GestureResponderEvent) => void onPress?: (e: GestureResponderEvent) => void
/**
* Web-only attribute. Sets `download` attr on web.
*/
download?: string
} }
export function useLink({ export function useLink({
@@ -156,12 +166,7 @@ export function useLink({
} }
export type LinkProps = Omit<BaseLinkProps, 'warnOnMismatchingTextChild'> & export type LinkProps = Omit<BaseLinkProps, 'warnOnMismatchingTextChild'> &
Omit<ButtonProps, 'onPress' | 'disabled' | 'label'> & { Omit<ButtonProps, 'onPress' | 'disabled' | 'label'>
/**
* Label for a11y. Defaults to the href.
*/
label?: string
}
/** /**
* A interactive element that renders as a `<a>` tag on the web. On mobile it * A interactive element that renders as a `<a>` tag on the web. On mobile it
@@ -176,6 +181,7 @@ export function Link({
to, to,
action = 'push', action = 'push',
onPress: outerOnPress, onPress: outerOnPress,
download,
...rest ...rest
}: LinkProps) { }: LinkProps) {
const {href, isExternal, onPress} = useLink({ const {href, isExternal, onPress} = useLink({
@@ -193,11 +199,12 @@ export function Link({
role="link" role="link"
accessibilityRole="link" accessibilityRole="link"
href={href} href={href}
onPress={onPress} onPress={download ? undefined : onPress}
{...web({ {...web({
hrefAttrs: { hrefAttrs: {
target: isExternal ? 'blank' : undefined, target: isExternal ? 'blank' : undefined,
rel: isExternal ? 'noopener noreferrer' : undefined, rel: isExternal ? 'noopener noreferrer' : undefined,
download,
}, },
dataSet: { dataSet: {
// no underline, only `InlineLink` has underlines // no underline, only `InlineLink` has underlines
@@ -210,13 +217,7 @@ export function Link({
} }
export type InlineLinkProps = React.PropsWithChildren< export type InlineLinkProps = React.PropsWithChildren<
BaseLinkProps & BaseLinkProps & TextStyleProp
TextStyleProp & {
/**
* Label for a11y. Defaults to the href.
*/
label?: string
}
> >
export function InlineLink({ export function InlineLink({
@@ -226,6 +227,7 @@ export function InlineLink({
warnOnMismatchingTextChild, warnOnMismatchingTextChild,
style, style,
onPress: outerOnPress, onPress: outerOnPress,
download,
...rest ...rest
}: InlineLinkProps) { }: InlineLinkProps) {
const t = useTheme() const t = useTheme()
@@ -253,7 +255,7 @@ export function InlineLink({
return ( return (
<TouchableWithoutFeedback <TouchableWithoutFeedback
accessibilityRole="button" accessibilityRole="button"
onPress={onPress} onPress={download ? undefined : onPress}
onPressIn={onPressIn} onPressIn={onPressIn}
onPressOut={onPressOut} onPressOut={onPressOut}
onFocus={onFocus} onFocus={onFocus}
@@ -279,6 +281,7 @@ export function InlineLink({
hrefAttrs: { hrefAttrs: {
target: isExternal ? 'blank' : undefined, target: isExternal ? 'blank' : undefined,
rel: isExternal ? 'noopener noreferrer' : undefined, rel: isExternal ? 'noopener noreferrer' : undefined,
download,
}, },
dataSet: { dataSet: {
// default to no underline, apply this ourselves // default to no underline, apply this ourselves