Skip to content

Commit

Permalink
refactor: remove conditional ThemeProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
sapkra committed Mar 27, 2024
1 parent f449757 commit 9024165
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ export * from '@fragment-build/ui';

Now just import all the components you need from this file.

Additionally you need to pass the `useNextJS` prop to the `FragmentUIProvider`.

## 🤝 Contributing

Feel free to fork and make a Pull Request to this project. All the input is warmly welcome!
Expand Down
12 changes: 5 additions & 7 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { FragmentUIContext, defaultContext } from "./context";

interface FragmentUIProviderProps {
children: React.ReactNode;
useNextJS?: boolean;
locale?: NextUIProviderProps['locale'];
navigate?: NextUIProviderProps['navigate'];
defaults?: FragmentUIContext['defaults']
}

export const FragmentUIProvider: React.FC<FragmentUIProviderProps> = ({
children,
useNextJS = false,
defaults = {},
...rest
}) => {
Expand All @@ -24,13 +22,13 @@ export const FragmentUIProvider: React.FC<FragmentUIProviderProps> = ({
setContext((oldContext) => deepMerge(oldContext, { defaults }))
}, [defaults])

const render = useNextJS
? <ThemeProvider attribute="class">{children}</ThemeProvider>
: children;

return (
<FragmentUIContext.Provider value={context}>
<NextUIProvider {...rest}>{render}</NextUIProvider>
<NextUIProvider {...rest}>
<ThemeProvider attribute="class">
{children}
</ThemeProvider>
</NextUIProvider>
</FragmentUIContext.Provider>
);
}
17 changes: 9 additions & 8 deletions src/withFragment.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { useContext } from "react";
import { forwardRef, useContext } from "react";
import deepMerge from "deepmerge";
import { FragmentUIContext } from "./context";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function withFragment<P extends React.ComponentType<any>>(Component: P, configId: keyof FragmentUIContext['defaults']): P {
const ComponentWithContext = (props: React.ComponentProps<P>) => {
export function withFragment<C extends React.ComponentType<any>>(Component: C, configId: keyof FragmentUIContext['defaults']): C {
const ComponentWithContext = forwardRef((props: React.ComponentProps<C>, ref) => {
const context = useContext(FragmentUIContext);
return <Component {...deepMerge<P>(context['defaults'][configId] as React.ComponentProps<P> || {}, props) as React.ComponentProps<P>}/>
};
return <Component {...deepMerge(context['defaults'][configId] as React.ComponentProps<C> || {}, props)} ref={ref} />
});

// fix react-aria collection
if ('getCollectionNode' in Component) {
(ComponentWithContext as typeof Component).getCollectionNode = Component.getCollectionNode;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(ComponentWithContext as any).getCollectionNode = Component.getCollectionNode;
}

return ComponentWithContext as P;
}
return ComponentWithContext as unknown as C;
}

0 comments on commit 9024165

Please sign in to comment.