Skip to content

Commit 6417c98

Browse files
authored
improve types, refactor A and Button, small tweaks (#2102)
1 parent 4d00dcf commit 6417c98

File tree

13 files changed

+124
-141
lines changed

13 files changed

+124
-141
lines changed

bun.lock

Lines changed: 59 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/styleguide.tsx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import Link from 'next/link';
44
import { type ComponentType, type CSSProperties, type PropsWithChildren, useState } from 'react';
55
import {
66
StyleSheet,
7-
type TextStyle,
8-
View,
7+
Pressable,
98
useWindowDimensions,
109
type StyleProp,
1110
type ViewStyle,
11+
type PressableProps,
1212
} from 'react-native';
13+
import { type Style } from 'twrnc';
1314

1415
import tw from '~/util/tailwind';
1516

@@ -42,7 +43,7 @@ type CustomTextProps = TextProps &
4243

4344
export function createTextComponent(
4445
Element: ComponentType<TextProps>,
45-
textStyle?: StyleProp<TextStyle>
46+
textStyle?: StyleProp<Style>
4647
) {
4748
function TextComponent({ children, style, id, numberOfLines }: CustomTextProps) {
4849
const elementStyle = Element?.displayName
@@ -55,7 +56,7 @@ export function createTextComponent(
5556
numberOfLines={numberOfLines}
5657
style={[
5758
tw`font-sans font-normal my-0 text-black dark:text-white`,
58-
elementStyle as StyleProp<TextStyle>,
59+
elementStyle as StyleProp<Style>,
5960
textStyle,
6061
style,
6162
]}>
@@ -81,10 +82,10 @@ export const Caption = createTextComponent(HtmlElements.P, textStyles.caption);
8182
export const Label = createTextComponent(HtmlElements.P, textStyles.label);
8283

8384
type AProps = PropsWithChildren<{
84-
style?: StyleProp<TextStyle>;
85+
style?: StyleProp<Style>;
8586
target?: string;
8687
href: string;
87-
hoverStyle?: StyleProp<TextStyle>;
88+
hoverStyle?: StyleProp<Style>;
8889
containerStyle?: CSSProperties;
8990
}>;
9091

@@ -98,6 +99,7 @@ export function A({ href, target, children, style, hoverStyle, containerStyle, .
9899
const passedStyle = StyleSheet.flatten(style);
99100
return (
100101
<Link
102+
{...rest}
101103
href={href}
102104
onPointerEnter={() => setIsHovered(true)}
103105
onPointerLeave={() => setIsHovered(false)}
@@ -130,26 +132,23 @@ export function A({ href, target, children, style, hoverStyle, containerStyle, .
130132
);
131133
}
132134

133-
type HoverEffectProps = PropsWithChildren<{ style?: StyleProp<ViewStyle> }>;
134-
135-
export function HoverEffect({ children, style }: HoverEffectProps) {
136-
const [isHovered, setIsHovered] = useState(false);
137-
const [isActive, setIsActive] = useState(false);
135+
type HoverEffectProps = PressableProps & { style?: StyleProp<ViewStyle> };
138136

137+
export function HoverEffect({ children, style, onPress, ...rest }: HoverEffectProps) {
139138
return (
140-
<View
141-
style={[
139+
<Pressable
140+
style={({ hovered, pressed }) => [
142141
{ transition: 'opacity 0.33s' },
143-
isHovered && tw`opacity-75`,
144-
isActive && tw`opacity-50`,
142+
hovered && tw`opacity-75`,
143+
pressed && tw`opacity-50`,
145144
style,
146145
]}
147-
onPointerEnter={() => setIsHovered(true)}
148-
onPointerLeave={() => setIsHovered(false)}
149-
onPointerDown={() => setIsActive(true)}
150-
onPointerUp={() => setIsActive(false)}
151-
accessible={false}>
146+
accessible={false}
147+
focusable={false}
148+
tabIndex={onPress ? 0 : -1}
149+
onPress={onPress}
150+
{...rest}>
152151
{children}
153-
</View>
152+
</Pressable>
154153
);
155154
}

components/Button.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { A } from '@expo/html-elements';
21
import { type PropsWithChildren } from 'react';
3-
import { type TextStyle, Pressable, type StyleProp, type ViewStyle } from 'react-native';
2+
import { type TextStyle, type StyleProp, type ViewStyle, View } from 'react-native';
3+
import { type Style } from 'twrnc';
44

5-
import { HoverEffect, P } from '~/common/styleguide';
5+
import { A, HoverEffect, P } from '~/common/styleguide';
66
import tw from '~/util/tailwind';
77

88
type Props = PropsWithChildren & {
99
href?: string;
1010
onPress?: () => void;
1111
openInNewTab?: boolean;
1212
style?: StyleProp<TextStyle>;
13-
containerStyle?: StyleProp<ViewStyle>;
14-
tabIndex?: number;
13+
containerStyle?: StyleProp<Style>;
1514
};
1615

1716
export function Button({
@@ -21,34 +20,35 @@ export function Button({
2120
style,
2221
containerStyle,
2322
openInNewTab,
24-
tabIndex,
2523
...rest
2624
}: Props) {
2725
const isLink = !!href;
2826
const buttonStyle = [
29-
tw`justify-center items-center rounded outline-offset-1 select-none bg-primary-darker dark:bg-primary-dark`,
27+
tw`justify-center items-center rounded outline-offset-1 cursor-pointer select-none bg-primary-darker dark:bg-primary-dark`,
3028
style,
31-
];
29+
] as ViewStyle[];
3230

3331
const content = typeof children === 'string' ? <P>{children}</P> : children;
3432

3533
return (
36-
<HoverEffect style={containerStyle}>
34+
<HoverEffect
35+
style={containerStyle}
36+
onPress={isLink ? undefined : onPress}
37+
tabIndex={isLink ? -1 : 0}>
3738
{isLink ? (
3839
<A
3940
href={href}
40-
style={tw`rounded font-sans`}
41-
tabIndex={tabIndex}
41+
style={[tw`rounded font-sans`, isLink && containerStyle]}
4242
{...(openInNewTab ? { target: '_blank' } : {})}
4343
{...rest}>
44-
<Pressable focusable={false} style={buttonStyle} accessible={false}>
44+
<View focusable={false} style={buttonStyle} accessible={false}>
4545
{content}
46-
</Pressable>
46+
</View>
4747
</A>
4848
) : (
49-
<Pressable onPress={onPress} style={buttonStyle} role="button" {...rest}>
49+
<View role="button" focusable={false} style={buttonStyle} accessible={false} {...rest}>
5050
{content}
51-
</Pressable>
51+
</View>
5252
)}
5353
</HoverEffect>
5454
);

components/Footer/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ export default function Footer() {
8989
<View style={[tw`self-center`, isSmallScreen && tw`items-center mt-6`]}>
9090
<A
9191
href="https://vercel.com/?utm_source=rndir&utm_campaign=oss"
92-
aria-label="Vercel banner">
92+
aria-label="Vercel banner"
93+
style={tw`flex`}>
9394
<VercelBanner />
9495
</A>
9596
</View>

components/Library/ConfigPlugin.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { type StyleProp, type TextStyle } from 'react-native';
1+
import { type StyleProp } from 'react-native';
2+
import { type Style } from 'twrnc';
23

34
import { A, P } from '~/common/styleguide';
45
import { type LibraryDataEntryType } from '~/types';
56

67
type Props = {
78
configPlugin?: LibraryDataEntryType['configPlugin'];
8-
hoverStyle?: StyleProp<TextStyle>;
9-
linkStyles?: StyleProp<TextStyle>;
10-
paragraphStyles?: StyleProp<TextStyle>;
9+
hoverStyle?: StyleProp<Style>;
10+
linkStyles?: StyleProp<Style>;
11+
paragraphStyles?: StyleProp<Style>;
1112
};
1213

1314
export function ConfigPluginContent({

components/Library/TrendingMark.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { View, type ViewStyle } from 'react-native';
1+
import { View, type ViewStyle, type TextStyle } from 'react-native';
2+
import { type Style } from 'twrnc';
23

3-
import { P, A, HoverEffect } from '~/common/styleguide';
4+
import { A, P, HoverEffect } from '~/common/styleguide';
45
import { type LibraryType } from '~/types';
56
import { getPopularityGrade } from '~/util/scoring';
67
import tw from '~/util/tailwind';
78

89
type Props = {
910
library: LibraryType | { popularity: number };
1011
markOnly?: boolean;
11-
style?: ViewStyle;
12+
style?: TextStyle | ViewStyle;
1213
};
1314

1415
export default function TrendingMark({ library, style, markOnly = false }: Props) {
@@ -45,10 +46,10 @@ export default function TrendingMark({ library, style, markOnly = false }: Props
4546
);
4647

4748
return markOnly ? (
48-
<View style={[tw`mb-1`, style]}>{content}</View>
49+
<View style={[tw`mb-1`, style as ViewStyle]}>{content}</View>
4950
) : (
5051
<HoverEffect>
51-
<A href="/scoring" style={[tw`flex relative items-start no-underline`, style]}>
52+
<A href="/scoring" style={[tw`flex relative items-start no-underline`, style as Style]}>
5253
{content}
5354
</A>
5455
</HoverEffect>

components/Package/EntityCounter.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { type StyleProp, type TextStyle } from 'react-native';
1+
import { type StyleProp } from 'react-native';
2+
import { type Style } from 'twrnc';
23

34
import { Label } from '~/common/styleguide';
45
import tw from '~/util/tailwind';
56

67
type Props = {
78
count: number;
8-
style?: StyleProp<TextStyle>;
9+
style?: StyleProp<Style>;
910
};
1011

1112
export default function EntityCounter({ count, style }: Props) {

components/Package/ReadmeCodeBlock.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ type Props = {
1111
theme: Theme;
1212
};
1313

14+
const SHIKI_OPTS = { langAlias: { gradle: 'groovy' } } as const;
15+
1416
export default function ReadmeCodeBlock({ code, theme, lang }: Props) {
15-
const highlighter = useShikiHighlighter(code, lang, theme, {
16-
langAlias: { gradle: 'groovy' },
17-
});
17+
const highlighter = useShikiHighlighter(code, lang, theme, SHIKI_OPTS);
1818

1919
const copyButton = (
2020
<Tooltip

components/Package/TrustedBadge.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function TrustedBadge() {
88
return (
99
<Tooltip
1010
trigger={
11-
<View>
11+
<View role="button" aria-label="Trusted publisher">
1212
<Verified width={16} height={16} style={tw`text-success`} />
1313
</View>
1414
}>

components/TopBar.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export default function TopBar() {
4747
<Button
4848
aria-label="Toggle theme"
4949
onPress={toggleTheme}
50-
style={tw`size-8.5 px-1 bg-transparent rounded-full`}>
50+
style={tw`size-8.5 px-1 bg-transparent`}
51+
containerStyle={tw`rounded-full`}>
5152
{tw.prefixMatch('dark') ? (
5253
<ThemeLight style={tw`text-white`} />
5354
) : (
@@ -64,8 +65,8 @@ export default function TopBar() {
6465
<Button
6566
aria-label="Tools"
6667
href="/tools"
67-
style={tw`size-8.5 px-1 bg-transparent rounded-full`}
68-
tabIndex={-1}>
68+
style={tw`size-8.5 px-1 bg-transparent`}
69+
containerStyle={tw`rounded-full`}>
6970
<Tools style={tw`text-white`} />
7071
</Button>
7172
</View>
@@ -79,8 +80,8 @@ export default function TopBar() {
7980
openInNewTab
8081
aria-label="GitHub repository"
8182
href="https://github.com/react-native-community/directory"
82-
style={tw`size-8.5 px-1 bg-transparent rounded-full`}
83-
tabIndex={-1}>
83+
style={tw`size-8.5 px-1 bg-transparent`}
84+
containerStyle={tw`rounded-full`}>
8485
<GitHub style={tw`text-white`} />
8586
</Button>
8687
</View>

0 commit comments

Comments
 (0)