From 4b155d553536d975f5f7b213a0b88c54327be816 Mon Sep 17 00:00:00 2001 From: doong-jo Date: Sat, 17 Feb 2024 22:53:53 +0900 Subject: [PATCH] Configure tailwind config --- readme.md | 11 +++++++++++ src/configure.ts | 11 +++++++++++ src/index.ts | 12 ++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/configure.ts diff --git a/readme.md b/readme.md index 929fc4e..77ad368 100644 --- a/readme.md +++ b/readme.md @@ -180,6 +180,17 @@ You can use `twrnc` right out of the box if you haven't customized your tailwind customizations you'd like to use. For that reason, we expose the ability to create a **custom configured version** of the `tw` function object. +### Initialize + +```js +// main.js (When the program will be initialized) +import { configure } from 'twrnc'; + +configure(require(`./tailwind.config.js`)); +``` + +### Create your own tw function + ```js // lib/tailwind.js import { create } from 'twrnc'; diff --git a/src/configure.ts b/src/configure.ts new file mode 100644 index 0000000..5de0687 --- /dev/null +++ b/src/configure.ts @@ -0,0 +1,11 @@ +import type { TwConfig } from './tw-config'; + +let initialTwConfig: TwConfig | undefined; + +export function getInitialTwConfig(): TwConfig | undefined { + return initialTwConfig; +} + +export function configure(twConfig: TwConfig): void { + initialTwConfig = twConfig; +} diff --git a/src/index.ts b/src/index.ts index 6d0b703..2e5a18c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,9 +3,17 @@ import type { TailwindFn, RnColorScheme, ClassInput, Style } from './types'; import type { TwConfig } from './tw-config'; import plugin from './plugin'; import rawCreate from './create'; +import { getInitialTwConfig } from './configure'; -// Apply default config and inject RN Platform -const create = (twConfig: TwConfig = {}): TailwindFn => rawCreate(twConfig, Platform.OS); +const initialTwConfig = getInitialTwConfig(); + +/** + * Apply default config and inject RN Platform + * If no config is provided, use the initial config + */ +const create = (twConfig: TwConfig = initialTwConfig || {}): TailwindFn => { + return rawCreate(twConfig, Platform.OS); +}; export type { TailwindFn, TwConfig, RnColorScheme, ClassInput, Style }; export { useDeviceContext, useAppColorScheme } from './hooks';