Skip to content

Commit

Permalink
Add Box component
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhabib committed Jun 5, 2024
1 parent b94b297 commit b3a29c6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@vanilla-extract/webpack-plugin": "^2.3.6",
"babel-loader": "^9.1.0",
"classnames": "^2.3.2",
"clsx": "^2.1.1",
"codemirror": "^5.65.10",
"command-line-args": "^5.2.1",
"command-line-usage": "^6.1.3",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/Playroom/Box/Box.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import clsx, { type ClassValue } from 'clsx';
import type { AllHTMLAttributes, ElementType } from 'react';
import { sprinkles, type Sprinkles } from '../sprinkles.css';

interface BoxProps
extends Omit<
AllHTMLAttributes<HTMLElement>,
'width' | 'height' | 'className' | 'data'
>,
Sprinkles {
className?: ClassValue;
component?: ElementType;
}

export const Box = ({
component = 'div',
className,
...restProps
}: BoxProps) => {
const atomProps: Record<string, unknown> = {};

for (const key in restProps) {
if (sprinkles.properties.has(key as keyof Sprinkles)) {
atomProps[key] = restProps[key as keyof typeof restProps];
delete restProps[key as keyof typeof restProps];
}
}

const userClasses = clsx(className);
const atomClasses = clsx(sprinkles({ ...atomProps }));
const classes = clsx(userClasses, atomClasses);

const Component = component;

return <Component className={classes} {...restProps} />;
};
2 changes: 2 additions & 0 deletions src/Playroom/sprinkles.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ const responsiveProperties = defineProperties({
});

export const sprinkles = createSprinkles(responsiveProperties);

export type Sprinkles = Parameters<typeof sprinkles>[0];

0 comments on commit b3a29c6

Please sign in to comment.