Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up twrnc theming for React Native component library #111

Merged
merged 32 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9ec0a61
Implemented themed twrnc
brianacnguyen Nov 13, 2024
db2ea35
Draft for twrnc implementation
brianacnguyen Nov 13, 2024
540a7e7
Updated dependencies
brianacnguyen Nov 14, 2024
8530131
Updated twrnc dep
brianacnguyen Nov 14, 2024
a73166e
Cleaned up Theme namings
brianacnguyen Nov 14, 2024
6dfe1f9
Updated yarn setup script to include build
brianacnguyen Nov 14, 2024
2e8640b
Merge branch 'main' of https://github.com/MetaMask/metamask-design-sy…
brianacnguyen Nov 14, 2024
bf3e039
Updated dependencies and imports
brianacnguyen Nov 14, 2024
b76a8fd
Added temp -passWithNoTests
brianacnguyen Nov 14, 2024
a44b8da
Updated temp yarn constraints update
brianacnguyen Nov 14, 2024
c1e7e47
Added withThemeProvider test
brianacnguyen Nov 14, 2024
227faf3
Updated test configs
brianacnguyen Nov 14, 2024
252c10d
Updated tests
brianacnguyen Nov 14, 2024
cc4f406
Updated more tests
brianacnguyen Nov 14, 2024
101216f
Updated tests
brianacnguyen Nov 14, 2024
11eba66
Updated withThemeProvider ref
brianacnguyen Nov 14, 2024
d17bdcf
Added props test to withthemeprovider
brianacnguyen Nov 14, 2024
73051b4
Updated withThemeProvider tests
brianacnguyen Nov 14, 2024
595edfc
Updated missed changes
brianacnguyen Nov 14, 2024
b9e985f
Updated twrnc-dependencies
brianacnguyen Nov 14, 2024
aebe56e
Updated twrnc in depcheck
brianacnguyen Nov 15, 2024
7501cc2
Merge branch 'main' into twrnc-package
brianacnguyen Nov 15, 2024
640a871
Moved stories file to ds-rn
brianacnguyen Nov 15, 2024
307dfaf
Merge branch 'twrnc-package' of https://github.com/MetaMask/metamask-…
brianacnguyen Nov 15, 2024
bb830ff
Merge branch 'main' into twrnc-package
brianacnguyen Nov 15, 2024
1a4d78b
Moving twrnc-preset under peerdependencies
brianacnguyen Nov 15, 2024
bfdc92d
Merge branch 'twrnc-package' of https://github.com/MetaMask/metamask-…
brianacnguyen Nov 15, 2024
86ededd
Updated dependencies
brianacnguyen Nov 15, 2024
9008d75
Updated yarn lock
brianacnguyen Nov 15, 2024
37e4def
Updated dependencies again
brianacnguyen Nov 15, 2024
8ddd59b
Removed unneccessary jest config updates
brianacnguyen Nov 15, 2024
d61740b
Updated depcheck
brianacnguyen Nov 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/storybook-react-native/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ module.exports = {
'babel.config.js',
'metro.config.js',
'jest.config.js',
'tailwind.config.js',
],
rules: {
'import/extensions': [
'error',
'ignorePackages',
{
js: 'always',
jsx: 'never',
jsx: 'always',
brianacnguyen marked this conversation as resolved.
Show resolved Hide resolved
ts: 'never',
tsx: 'never',
},
Expand Down
12 changes: 12 additions & 0 deletions apps/storybook-react-native/.storybook/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { addDecorator } from '@storybook/react-native';
import {
Theme,
ThemeProvider,
ColorScheme,
} from '@metamask/design-system-react-native';
brianacnguyen marked this conversation as resolved.
Show resolved Hide resolved

addDecorator((Story) => (
<ThemeProvider theme={Theme.Brand} colorScheme={ColorScheme.Themed}>
<Story />
</ThemeProvider>
));
2 changes: 2 additions & 0 deletions apps/storybook-react-native/.storybook/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as SystemUI from 'expo-system-ui';
import './config';
import { getStorybookUI } from '@storybook/react-native';
import registerRootComponent from 'expo/build/launch/registerRootComponent';
import './storybook.requires'; // Ensure this file exists and the path is correct
Expand Down
24 changes: 12 additions & 12 deletions apps/storybook-react-native/.storybook/storybook.requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ import {
addParameters,
addArgsEnhancer,
clearDecorators,
} from '@storybook/react-native';
} from "@storybook/react-native";

global.STORIES = [
{
titlePrefix: '',
directory: './src',
files: '**/*.stories.?(ts|tsx)',
titlePrefix: "",
directory: "./src",
files: "**/*.stories.?(ts|tsx)",
importPathMatcher:
'^\\.[\\\\/](?:src(?:\\/(?!\\.)(?:(?:(?!(?:^|\\/)\\.).)*?)\\/|\\/|$)(?!\\.)(?=.)[^/]*?\\.stories\\.(?:ts|tsx)?)$',
"^\\.[\\\\/](?:src(?:\\/(?!\\.)(?:(?:(?!(?:^|\\/)\\.).)*?)\\/|\\/|$)(?!\\.)(?=.)[^/]*?\\.stories\\.(?:ts|tsx)?)$",
},
];

import '@storybook/addon-ondevice-controls/register';
import '@storybook/addon-ondevice-actions/register';
import "@storybook/addon-ondevice-controls/register";
import "@storybook/addon-ondevice-actions/register";

import { argsEnhancers } from '@storybook/addon-actions/dist/modern/preset/addArgs';
import { argsEnhancers } from "@storybook/addon-actions/dist/modern/preset/addArgs";

import { decorators, parameters } from './preview';
import { decorators, parameters } from "./preview";

if (decorators) {
if (__DEV__) {
// stops the warning from showing on every HMR
require('react-native').LogBox.ignoreLogs([
'`clearDecorators` is deprecated and will be removed in Storybook 7.0',
require("react-native").LogBox.ignoreLogs([
"`clearDecorators` is deprecated and will be removed in Storybook 7.0",
]);
}
// workaround for global decorators getting infinitely applied on HMR, see https://github.com/storybookjs/react-native/issues/185
Expand All @@ -46,7 +46,7 @@ try {
} catch {}

const getStories = () => {
return { './src/Button.stories.tsx': require('../src/Button.stories.tsx') };
return { "./src/Button.stories.tsx": require("../src/Button.stories.tsx") };
};

configure(getStories, module, false);
20 changes: 1 addition & 19 deletions apps/storybook-react-native/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@
"slug": "storybook-react-native",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
}
},
"web": {
"favicon": "./assets/favicon.png"
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unneeded things

"userInterfaceStyle": "automatic"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For expo's theme switching capability

}
}
Binary file not shown.
Binary file removed apps/storybook-react-native/assets/favicon.png
Binary file not shown.
Binary file removed apps/storybook-react-native/assets/icon.png
Binary file not shown.
Binary file removed apps/storybook-react-native/assets/splash.png
Binary file not shown.
3 changes: 3 additions & 0 deletions apps/storybook-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
},
"dependencies": {
"@metamask/design-system-react-native": "workspace:^",
"@metamask/design-tokens": "^4.0.0",
"expo": "~49.0.23",
"expo-status-bar": "~1.6.0",
"expo-system-ui": "~2.4.0",
"react-native-reanimated": "3.3.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for dark mode

"twrnc": "^4.5.1",
"uuid": "^3.0.0"
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions apps/storybook-react-native/src/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button } from '@metamask/design-system-react-native';
import { Button, useTailwind } from '@metamask/design-system-react-native';
import type { Meta, StoryObj } from '@storybook/react-native';
import React from 'react';
import { View } from 'react-native';
Expand All @@ -15,8 +15,9 @@ export default ButtonMeta;

export const Basic: StoryObj<typeof Button> = {
render: () => {
const tw = useTailwind();
return (
<View>
<View style={tw`bg-primary-default p-3`}>
<Button text="Sample Button" />
</View>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this here to show the theme switching colors

);
Expand Down
11 changes: 11 additions & 0 deletions apps/storybook-react-native/tailwind.config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare const config: {
content: string[];
theme: {
extend: {
colors: Record<string, string>;
};
};
plugins: unknown[];
};

export default config;
9 changes: 9 additions & 0 deletions apps/storybook-react-native/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
content: ['./src/**/*.{js,jsx,ts,tsx}'],
theme: {
extend: {
colors: {},
},
},
plugins: [],
};
3 changes: 2 additions & 1 deletion apps/storybook-react-native/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"babel.config.js",
"metro.config.js",
"jest.config.js",
".eslintrc.js"
".eslintrc.js",
"tailwind.config.js"
],
"extends": "expo/tsconfig.base"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"semver": "^7.6.3",
"simple-git-hooks": "^2.8.0",
"ts-node": "^10.9.1",
"twrnc": "^4.5.1",
brianacnguyen marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "~5.2.2",
"yargs": "^17.7.2"
},
Expand Down
9 changes: 7 additions & 2 deletions packages/design-system-react-native/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ module.exports = {
},
},
},
ignorePatterns: ['.eslintrc.js', 'jest.config.js', 'babel.config.js'],
ignorePatterns: [
'.eslintrc.js',
'jest.config.js',
'babel.config.js',
'tailwind.config.js',
],

rules: {
'import/extensions': [
'error',
'ignorePackages',
{
js: 'always',
jsx: 'never',
jsx: 'always',
ts: 'never',
tsx: 'never',
},
Expand Down
10 changes: 8 additions & 2 deletions packages/design-system-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dist/"
],
"scripts": {
"build": "tsc --project tsconfig.build.json",
"build": "rm -rf tsconfig.build.tsbuildinfo dist && tsc --traceResolution --project tsconfig.build.json",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/design-system-react-native",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/design-system-react-native",
Expand All @@ -42,7 +42,11 @@
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
},
"dependencies": {
"@metamask/design-system-twrnc-preset": "workspace:^"
},
brianacnguyen marked this conversation as resolved.
Show resolved Hide resolved
"devDependencies": {
"@babel/core": "^7.23.5",
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.25.9",
"@babel/preset-typescript": "^7.23.3",
Expand All @@ -62,8 +66,10 @@
"typescript": "~5.2.2"
},
"peerDependencies": {
"@metamask/design-tokens": "^4.0.0",
"react": "^18.2.0",
"react-native": "0.72.15"
"react-native": "0.72.15",
"twrnc": "^4.5.1"
},
"engines": {
"node": "^18.18 || >=20"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useTailwind } from '@metamask/design-system-twrnc-preset';
import React from 'react';
import { TouchableOpacity, Text } from 'react-native';

export type ButtonProps = {
text: string;
};

const Button = ({ text }: ButtonProps) => {
const tw = useTailwind();
return (
<TouchableOpacity>
<TouchableOpacity style={tw`bg-primary-default`}>
<Text>{text}</Text>
</TouchableOpacity>
);
Expand Down
37 changes: 37 additions & 0 deletions packages/design-system-react-native/src/hocs/withThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable jsdoc/require-returns */
// src/hocs/withThemeProvider.tsx

import {
ThemeProvider,
ThemeContext,
Theme,
ColorScheme,
} from '@metamask/design-system-twrnc-preset';
import React, { forwardRef, useContext } from 'react';

/**
* HOC to wrap components with ThemeProvider if none is present.
* @param Component - The component to wrap with ThemeProvider.
*/
export function withThemeProvider<Props extends object>(
Component: React.ComponentType<Props>,
) {
const WrappedComponent = forwardRef<unknown, Props>((props, ref) => {
// Check if a ThemeProvider is already present
const themeContext = useContext(ThemeContext);

// If ThemeProvider exists, use the component as is
if (themeContext) {
return <Component {...(props as Props)} ref={ref} />;
}

// Otherwise, wrap with ThemeProvider
return (
<ThemeProvider theme={Theme.Brand} colorScheme={ColorScheme.Themed}>
<Component {...(props as Props)} ref={ref} />
</ThemeProvider>
);
});

return WrappedComponent;
}
21 changes: 20 additions & 1 deletion packages/design-system-react-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
export { default as Button } from './components/Button';
// index.ts

import ButtonComponent from './components/Button';
import { withThemeProvider } from './hocs/withThemeProvider';

// Wrap the Button component
export const Button = withThemeProvider(ButtonComponent);

// Export other components similarly
// import OtherComponent from './components/OtherComponent';
// export const OtherComponent = withThemeProvider(OtherComponent);

export type { ThemeContextProps, ThemeProviderProps } from './provider';
export {
useTailwind,
ThemeContext,
ThemeProvider,
Theme,
ColorScheme,
} from './provider';
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Theme as DesignTokenTheme } from '@metamask/design-tokens';
import { lightTheme, darkTheme } from '@metamask/design-tokens';

import { Theme } from './Theme.types';

type ThemeColors = DesignTokenTheme['colors'];

type ThemeVariant = {
light: ThemeColors;
dark: ThemeColors;
};

type Themes = {
brand: ThemeVariant;
};

export const themes: Themes = {
[Theme.Brand]: {
light: lightTheme.colors,
dark: darkTheme.colors,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useContext } from 'react';

import { ThemeContext } from './Theme.providers';

export const useTailwind = () => {
const { tw } = useContext(ThemeContext);
return tw;
};
Loading
Loading