Skip to content

Commit bfa710c

Browse files
committed
feat: add config-provider component
1 parent 6c487ac commit bfa710c

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

docs/tsconfig.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2017",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": true,
8+
"noEmit": true,
9+
"esModuleInterop": true,
10+
"module": "esnext",
11+
"moduleResolution": "bundler",
12+
"resolveJsonModule": true,
13+
"isolatedModules": true,
14+
"jsx": "preserve",
15+
"incremental": true,
16+
"plugins": [
17+
{
18+
"name": "next"
19+
}
20+
],
21+
"paths": {
22+
"@/*": ["./*"]
23+
}
24+
},
25+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
26+
"exclude": ["node_modules"]
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use client';
2+
3+
import { ConfigContext } from './context';
4+
import type { ComponentConfig, ConfigProviderProps } from './types';
5+
6+
const COMPONENT_KEYS = ['accordion'] satisfies (keyof ComponentConfig)[];
7+
8+
export const ConfigProvider = (props: ConfigProviderProps) => {
9+
const { children, direction = 'ltr', size = 'md', theme = { color: 'default' }, ...rest } = props;
10+
11+
const componentConfig = Object.fromEntries(
12+
COMPONENT_KEYS.map(key => [
13+
key,
14+
{
15+
...rest[key],
16+
dir: direction,
17+
size
18+
}
19+
])
20+
);
21+
22+
return <ConfigContext.Provider value={componentConfig}>{children}</ConfigContext.Provider>;
23+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use client';
2+
3+
import { createContext, useContext } from 'react';
4+
5+
import type { ComponentConfig } from './types';
6+
7+
export const ConfigContext = createContext<ComponentConfig>({});
8+
9+
export const useComponentConfig = <T extends keyof ComponentConfig>(component: T) => {
10+
const config = useContext(ConfigContext);
11+
12+
if (!config) {
13+
throw new Error('useComponentConfig must be used within a ConfigProvider');
14+
}
15+
16+
if (!config?.[component]) return {};
17+
18+
return config[component];
19+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './ConfigProvider';
2+
3+
export * from './types';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { ThemeOptions } from '@soybean-react-ui/tailwind-plugin';
2+
3+
import type { ThemeSize } from '@/types/other';
4+
5+
import type { AccordionProps } from '../accordion/types';
6+
7+
export interface ConfigProviderProps extends ConfigProps {
8+
children: React.ReactNode;
9+
}
10+
11+
export interface ConfigProps extends ComponentConfig {
12+
direction?: 'ltr' | 'rtl';
13+
size?: ThemeSize;
14+
theme?: ThemeOptions;
15+
}
16+
17+
export type ComponentConfig = {
18+
accordion?: AccordionConfig;
19+
};
20+
21+
export type AccordionConfig = Pick<AccordionProps, 'className' | 'classNames' | 'dir' | 'size' | 'triggerIcon'>;

0 commit comments

Comments
 (0)