-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is nothing but just a wrapper component with special handling for transformed sx prop.
- Loading branch information
1 parent
eda9f79
commit 43dba01
Showing
8 changed files
with
139 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { BaseDefaultProps, Substitute, NoInfer } from './base'; | ||
import type { SxProp } from './sx'; | ||
|
||
export type PolymorphicComponentProps< | ||
SxProp, | ||
BaseProps extends object, | ||
AsTarget extends React.ElementType | undefined, | ||
AsTargetProps extends object = AsTarget extends React.ElementType | ||
? React.ComponentPropsWithRef<AsTarget> | ||
: BaseDefaultProps, | ||
> = NoInfer<Omit<Substitute<BaseProps, AsTargetProps>, 'as' | 'component'>> & { | ||
as?: AsTarget; | ||
component?: AsTarget; | ||
sx?: SxProp; | ||
children?: React.ReactNode; | ||
}; | ||
|
||
export interface PolymorphicComponent<SxProp, BaseProps extends BaseDefaultProps> | ||
extends React.ForwardRefExoticComponent<BaseProps> { | ||
<AsTarget extends React.ElementType | undefined = undefined>( | ||
props: PolymorphicComponentProps<SxProp, BaseProps, AsTarget>, | ||
): JSX.Element; | ||
} | ||
|
||
declare const Box: PolymorphicComponent<SxProp, {}>; | ||
|
||
export { Box }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as React from 'react'; | ||
|
||
// eslint-disable-next-line react/prop-types | ||
export const Box = React.forwardRef( | ||
( | ||
{ | ||
as = 'div', | ||
// Added to support compatibility with @mui/system | ||
component, | ||
/** | ||
* The type of the transformed sx prop is either a | ||
* "string" if the css passed was fully static or an | ||
* object with the following shape: | ||
* { | ||
* className: string, | ||
* vars: Record<string, [string | number, boolean]> | ||
* } | ||
*/ | ||
sx, | ||
className, | ||
style, | ||
...rest | ||
}, | ||
ref, | ||
) => { | ||
const Component = component ?? as; | ||
// eslint-disable-next-line react/prop-types | ||
const sxClass = typeof sx === 'string' ? sx : sx?.className; | ||
const classes = [className, sxClass].filter(Boolean).join(' '); | ||
// eslint-disable-next-line react/prop-types | ||
const sxVars = sx && typeof sx !== 'string' ? sx?.vars : {}; | ||
const varStyles = {}; | ||
|
||
if (sxVars) { | ||
Object.entries(sxVars).forEach(([cssVariable, [value, isUnitLess]]) => { | ||
if (typeof value === 'string' || isUnitLess) { | ||
varStyles[`--${cssVariable}`] = value; | ||
} else { | ||
varStyles[`--${cssVariable}`] = `${value}px`; | ||
} | ||
}); | ||
} | ||
|
||
const styles = { | ||
...style, | ||
...varStyles, | ||
}; | ||
|
||
return <Component ref={ref} className={classes} style={styles} {...rest} />; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as React from 'react'; | ||
import { Box } from '@pigment-css/react/Box'; | ||
|
||
export function App() { | ||
return ( | ||
<Box as="div" sx={{ display: 'flex' }}> | ||
<Box as="p" sx={() => ({ color: 'primary' })}> | ||
Hello{' '} | ||
<Box as="a" href="https://mui.com" download> | ||
Link | ||
</Box> | ||
<Box component="dialog" open> | ||
Dialog | ||
</Box> | ||
{/* @ts-expect-error */} | ||
<Box component="dialog" as="button" open> | ||
Dialog 2 | ||
</Box> | ||
</Box> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters