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 4 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
2 changes: 1 addition & 1 deletion apps/storybook-react-native/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
'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-twrnc-preset';

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.
4 changes: 4 additions & 0 deletions apps/storybook-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
},
"dependencies": {
"@metamask/design-system-react-native": "workspace:^",
"@metamask/design-system-twrnc-preset": "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
4 changes: 3 additions & 1 deletion apps/storybook-react-native/src/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Button } from '@metamask/design-system-react-native';
import { useTailwind } from '@metamask/design-system-twrnc-preset';
import type { Meta, StoryObj } from '@storybook/react-native';
import React from 'react';
import { View } from 'react-native';
Expand All @@ -15,8 +16,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" />
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

</View>
);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"packages/*"
],
"scripts": {
"build": "yarn ts-bridge --project tsconfig.build.json --verbose && yarn workspace @metamask/design-system-react-native build",
"build": "yarn ts-bridge --project tsconfig.build.json --verbose && yarn workspace @metamask/design-system-twrnc-preset build && yarn workspace @metamask/design-system-react-native build",
"build:clean": "rimraf dist '**/*.tsbuildinfo' && yarn build",
"build:docs": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run build:docs",
"build:types": "tsc --build tsconfig.build.json --verbose",
Expand Down 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
2 changes: 1 addition & 1 deletion packages/design-system-react-native/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
'ignorePackages',
{
js: 'always',
jsx: 'never',
jsx: 'always',
ts: 'never',
tsx: 'never',
},
Expand Down
8 changes: 6 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 --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 @@ -43,6 +43,7 @@
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
},
"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 +63,11 @@
"typescript": "~5.2.2"
},
"peerDependencies": {
"@metamask/design-system-twrnc-preset": "workspace:^",
"@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;
}
12 changes: 11 additions & 1 deletion packages/design-system-react-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
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);
6 changes: 5 additions & 1 deletion packages/design-system-react-native/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"types": ["react-native", "jest"],
"emitDeclarationOnly": false
},
"references": [],
"references": [
{
"path": "../design-system-twrnc-preset/tsconfig.build.json"
}
],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mcmire suggested to add this

"include": ["./src"]
}
6 changes: 5 additions & 1 deletion packages/design-system-react-native/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"jsx": "react-native",
"skipLibCheck": true
},
"references": [],
"references": [
{
"path": "../design-system-twrnc-preset"
}
],
"include": ["src/**/*"],
"exclude": ["jest.config.js", ".eslintrc.js", "babel.config.js"]
}
35 changes: 35 additions & 0 deletions packages/design-system-twrnc-preset/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
extends: ['../../.eslintrc.js'],
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
settings: {
'import/resolver': {
typescript: {
tsconfigRootDir: __dirname,
project: './tsconfig.json',
},
},
},
ignorePatterns: [
'.eslintrc.js',
'jest.config.js',
'babel.config.js',
'tailwind.config.js',
],

rules: {
'import/extensions': [
'error',
'ignorePackages',
{
js: 'always',
jsx: 'always',
ts: 'never',
tsx: 'never',
},
],
},
};
2 changes: 1 addition & 1 deletion packages/design-system-twrnc-preset/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `@metamask/design-system-twrnc-preset`

Design System twrnc preset
Design System React Native

## Installation

Expand Down
18 changes: 18 additions & 0 deletions packages/design-system-twrnc-preset/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// We use Babel for our tests in scripts/.
module.exports = {
env: {
test: {
presets: [
'module:metro-react-native-babel-preset',
'@babel/preset-env',
'@babel/preset-react',
'@babel/preset-typescript',
],
plugins: [
['@babel/plugin-transform-class-properties', { loose: true }],
['@babel/plugin-transform-private-methods', { loose: true }],
['@babel/plugin-transform-private-property-in-object', { loose: true }],
],
},
},
};
11 changes: 11 additions & 0 deletions packages/design-system-twrnc-preset/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ module.exports = merge(baseConfig, {
statements: 100,
},
},
preset: 'react-native',
transform: {
'^.+\\.(js|jsx|ts|tsx)$': 'babel-jest',
},
transformIgnorePatterns: [
'node_modules/(?!(react-native|@react-native|@react-navigation)/)',
],
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
moduleNameMapper: {
'\\.(css|less|scss)$': 'identity-obj-proxy',
},
});
Loading
Loading