Skip to content

Commit 69f221f

Browse files
committed
feat: support metadata extraction from wrapped components
1 parent 6f72990 commit 69f221f

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

packages/kstyled/src/utils/component-metadata.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export function isAnimatedComponent(component: ComponentType<unknown>): boolean
1313
/**
1414
* Extract metadata from a base component
1515
* Returns empty object for Animated components or if no metadata exists
16+
*
17+
* This function handles various component types:
18+
* - Direct styled components with __kstyled_metadata__
19+
* - forwardRef wrapped styled components
20+
* - React.memo wrapped styled components
1621
*/
1722
export function extractBaseMetadata(
1823
baseComponent: ComponentType<unknown>
@@ -21,10 +26,38 @@ export function extractBaseMetadata(
2126
return {};
2227
}
2328

29+
// Check if component has metadata directly
2430
if ('__kstyled_metadata__' in baseComponent) {
2531
return (baseComponent as ComponentWithMetadata).__kstyled_metadata__ || {};
2632
}
2733

34+
// Check if it's a forwardRef component (has $$typeof and render)
35+
// @ts-expect-error - Accessing internal React properties
36+
if (baseComponent.$$typeof && baseComponent.render) {
37+
// @ts-expect-error - Accessing internal React properties
38+
const renderFunc = baseComponent.render;
39+
if (renderFunc && '__kstyled_metadata__' in renderFunc) {
40+
return (renderFunc as ComponentWithMetadata).__kstyled_metadata__ || {};
41+
}
42+
}
43+
44+
// Check if it's a React.memo component (has $$typeof and type)
45+
// @ts-expect-error - Accessing internal React properties
46+
if (baseComponent.$$typeof && baseComponent.type) {
47+
// @ts-expect-error - Accessing internal React properties
48+
const wrappedComponent = baseComponent.type;
49+
if (wrappedComponent && '__kstyled_metadata__' in wrappedComponent) {
50+
return (wrappedComponent as ComponentWithMetadata).__kstyled_metadata__ || {};
51+
}
52+
// Also check if the wrapped component is a forwardRef
53+
if (wrappedComponent.$$typeof && wrappedComponent.render) {
54+
const renderFunc = wrappedComponent.render;
55+
if (renderFunc && '__kstyled_metadata__' in renderFunc) {
56+
return (renderFunc as ComponentWithMetadata).__kstyled_metadata__ || {};
57+
}
58+
}
59+
}
60+
2861
return {};
2962
}
3063

0 commit comments

Comments
 (0)