-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support branding * fix: make title also clickable refactor: add explanation to theme.brand * fix: typo * fix: typo * fix: logo not rendering on mobile and limit to just image source prop * fix: types * fix: allow react element * fix: update story * fix --------- Co-authored-by: Daniel Williams <[email protected]>
- Loading branch information
Showing
10 changed files
with
389 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { StoryObj, Meta } from '@storybook/react'; | ||
import { StorybookLogo } from './StorybookLogo'; | ||
import { Theme, theme } from '@storybook/react-native-theming'; | ||
import { Text } from 'react-native'; | ||
|
||
const meta = { | ||
component: StorybookLogo, | ||
title: 'UI/StorybookLogo', | ||
args: { | ||
theme: null, | ||
}, | ||
} satisfies Meta<typeof StorybookLogo>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const TitleLogo: Story = { | ||
args: { | ||
theme: { | ||
...theme, | ||
brand: { title: 'React Native' }, | ||
} satisfies Theme, | ||
}, | ||
}; | ||
|
||
export const ImageLogo: Story = { | ||
args: { | ||
theme: { | ||
...theme, | ||
brand: { | ||
image: { | ||
uri: 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/512px-React-icon.svg.png', | ||
height: 25, | ||
width: 25, | ||
}, | ||
}, | ||
} satisfies Theme, | ||
}, | ||
}; | ||
|
||
export const ImageUrlLogo: Story = { | ||
args: { | ||
theme: { | ||
...theme, | ||
brand: { | ||
image: { | ||
uri: 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/512px-React-icon.svg.png', | ||
width: 25, | ||
height: 25, | ||
}, | ||
title: 'React Native', | ||
url: 'https://reactnative.dev', | ||
}, | ||
} satisfies Theme, | ||
}, | ||
}; | ||
|
||
export const ImageSourceLogo: Story = { | ||
args: { | ||
theme: { | ||
...theme, | ||
brand: { | ||
image: require('./assets/react-native-logo.png'), | ||
resizeMode: 'contain', | ||
url: 'https://reactnative.dev', | ||
}, | ||
} satisfies Theme, | ||
}, | ||
}; | ||
|
||
export const ImageElementLogo: Story = { | ||
args: { | ||
theme: { ...theme, brand: { image: <Text>Element</Text> } } satisfies Theme, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Theme } from '@storybook/react-native-theming'; | ||
import { FC, isValidElement, ReactElement, useEffect, useMemo } from 'react'; | ||
import { Image, Linking, StyleProp, Text, TextStyle, TouchableOpacity } from 'react-native'; | ||
import { DarkLogo } from './icon/DarkLogo'; | ||
import { Logo } from './icon/Logo'; | ||
|
||
const WIDTH = 125; | ||
const HEIGHT = 25; | ||
|
||
const NoBrandLogo: FC<{ theme: Theme }> = ({ theme }) => | ||
theme.base === 'light' ? ( | ||
<Logo height={HEIGHT} width={WIDTH} /> | ||
) : ( | ||
<DarkLogo height={HEIGHT} width={WIDTH} /> | ||
); | ||
|
||
function isElement(value: unknown): value is ReactElement { | ||
return isValidElement(value); | ||
} | ||
|
||
const BrandLogo: FC<{ theme: Theme }> = ({ theme }) => { | ||
const imageHasNoWidthOrHeight = | ||
typeof theme.brand.image === 'object' && | ||
typeof theme.brand.image === 'object' && | ||
'uri' in theme.brand.image && | ||
(!('height' in theme.brand.image) || !('width' in theme.brand.image)); | ||
|
||
useEffect(() => { | ||
if (imageHasNoWidthOrHeight) { | ||
console.warn( | ||
"STORYBOOK: When using a remote image as the brand logo, you must also set the width and height.\nFor example: brand: { image: { uri: 'https://sb.com/img.png', height: 25, width: 25}}" | ||
); | ||
} | ||
}, [imageHasNoWidthOrHeight]); | ||
|
||
if (!theme.brand.image) { | ||
return null; | ||
} | ||
|
||
if (isElement(theme.brand.image)) { | ||
return theme.brand.image; | ||
} | ||
|
||
const image = ( | ||
<Image | ||
source={theme.brand.image} | ||
resizeMode={theme.brand.resizeMode ?? 'contain'} | ||
style={imageHasNoWidthOrHeight ? { width: WIDTH, height: HEIGHT } : undefined} | ||
/> | ||
); | ||
|
||
if (theme.brand.url) { | ||
return ( | ||
<TouchableOpacity | ||
onPress={() => { | ||
if (theme.brand.url) Linking.openURL(theme.brand.url); | ||
}} | ||
> | ||
{image} | ||
</TouchableOpacity> | ||
); | ||
} else { | ||
return image; | ||
} | ||
}; | ||
|
||
const BrandTitle: FC<{ theme: Theme }> = ({ theme }) => { | ||
const brandTitleStyle = useMemo<StyleProp<TextStyle>>(() => { | ||
return { | ||
width: WIDTH, | ||
height: HEIGHT, | ||
color: theme.color.defaultText, | ||
fontSize: theme.typography.size.m1, | ||
}; | ||
}, [theme]); | ||
|
||
const title = ( | ||
<Text style={brandTitleStyle} numberOfLines={1} ellipsizeMode="tail"> | ||
{theme.brand.title} | ||
</Text> | ||
); | ||
|
||
if (theme.brand.url) { | ||
return ( | ||
<TouchableOpacity | ||
onPress={() => { | ||
if (theme.brand.url) Linking.openURL(theme.brand.url); | ||
}} | ||
> | ||
{title} | ||
</TouchableOpacity> | ||
); | ||
} else { | ||
return title; | ||
} | ||
}; | ||
|
||
export const StorybookLogo: FC<{ theme: Theme }> = ({ theme }) => { | ||
if (theme.brand?.image) { | ||
return <BrandLogo theme={theme} />; | ||
} else if (theme.brand?.title) { | ||
return <BrandTitle theme={theme} />; | ||
} else { | ||
return <NoBrandLogo theme={theme} />; | ||
} | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.