Skip to content

Commit

Permalink
[TypeScript] Fix sx types and missing style prop from layout comp…
Browse files Browse the repository at this point in the history
…onents (#191)
  • Loading branch information
siriwatknp authored Aug 6, 2024
1 parent 69a550a commit 4576df1
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/pigment-css-react/src/Box.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type PolymorphicComponentProps<
AsTarget extends React.ElementType | undefined,
AsTargetProps extends object = AsTarget extends React.ElementType
? React.ComponentPropsWithRef<AsTarget>
: BaseDefaultProps,
: React.HTMLAttributes<HTMLElement>,
> = NoInfer<Omit<Substitute<BaseProps, AsTargetProps>, 'as' | 'component'>> & {
/**
* The component used for the root node.
Expand Down Expand Up @@ -37,6 +37,6 @@ export interface PolymorphicComponent<BaseProps extends BaseDefaultProps>
): React.JSX.Element;
}

declare const Box: PolymorphicComponent<{}>;
declare const Box: PolymorphicComponent<React.DetailsHTMLAttributes<HTMLDivElement>>;

export default Box;
4 changes: 3 additions & 1 deletion packages/pigment-css-react/src/Container.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type ContainerBaseProps = {
maxWidth?: Breakpoint | false;
};

declare const Container: PolymorphicComponent<ContainerBaseProps>;
declare const Container: PolymorphicComponent<
ContainerBaseProps & React.DetailsHTMLAttributes<HTMLDivElement>
>;

export default Container;
4 changes: 3 additions & 1 deletion packages/pigment-css-react/src/Grid.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type GridBaseProps = {
wrap?: 'nowrap' | 'wrap' | 'wrap-reverse';
};

declare const Grid: PolymorphicComponent<GridBaseProps>;
declare const Grid: PolymorphicComponent<
GridBaseProps & React.DetailsHTMLAttributes<HTMLDivElement>
>;

export default Grid;
4 changes: 3 additions & 1 deletion packages/pigment-css-react/src/Hidden.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface HiddenBaseProps extends HiddenUp, HiddenDown {
only?: Breakpoint | Breakpoint[];
}

declare const Hidden: PolymorphicComponent<HiddenBaseProps>;
declare const Hidden: PolymorphicComponent<
HiddenBaseProps & React.DetailsHTMLAttributes<HTMLDivElement>
>;

export default Hidden;
4 changes: 3 additions & 1 deletion packages/pigment-css-react/src/Stack.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type StackBaseProps = {
className?: string;
};

declare const Stack: PolymorphicComponent<StackBaseProps>;
declare const Stack: PolymorphicComponent<
StackBaseProps & React.DetailsHTMLAttributes<HTMLDivElement>
>;

export default Stack;
6 changes: 3 additions & 3 deletions packages/pigment-css-react/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ styled.div(({ theme }) => ({
}));

sx({ color: 'red' });
sx(({ theme }) => ({
sx((theme) => ({
color: theme.palette.primary.main,
}));
sx([
{ color: 'red' },
({ theme }) => ({
(theme) => ({
color: theme.palette.primary.main,
}),
]);
const foo = true;
sx([
true && { color: 'red' },
foo
? ({ theme }) => ({
? (theme) => ({
color: theme.palette.primary.main,
})
: {
Expand Down
5 changes: 4 additions & 1 deletion packages/pigment-css-react/src/sx.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { CSSObjectNoCallback } from './base';
import type { ThemeArgs } from './theme';

export type SxProp = CSSObjectNoCallback | ((themeArgs: ThemeArgs) => CSSObjectNoCallback);
export type SxProp =
| CSSObjectNoCallback
| ((themeArgs: ThemeArgs['theme']) => CSSObjectNoCallback)
| ReadonlyArray<CSSObjectNoCallback | ((themeArgs: ThemeArgs['theme']) => CSSObjectNoCallback)>;

export default function sx(arg: SxProp | Array<SxProp>, componentClass?: string): string;
4 changes: 2 additions & 2 deletions packages/pigment-css-react/tests/Box.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Box } from '@pigment-css/react/Box';
import Box from '../src/Box';

export function App() {
return (
Expand All @@ -13,7 +13,7 @@ export function App() {
Dialog
</Box>
{/* @ts-expect-error */}
<Box component="dialog" as="button" open>
<Box component="dialog" as="button" href>
Dialog 2
</Box>
</Box>
Expand Down
10 changes: 10 additions & 0 deletions packages/pigment-css-react/tests/Container.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from 'react';
import Container from '../src/Container';

<Container maxWidth="md" />;

<Container
className=""
style={{ accentColor: 'ActiveBorder', marginTop: '1rem' }}
data-testid="foo"
/>;
6 changes: 6 additions & 0 deletions packages/pigment-css-react/tests/Grid.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react';
import Grid from '../src/Grid';

<Grid size={{ xs: 2, md: 3, lg: 4 }} />;

<Grid className="" style={{ accentColor: 'ActiveBorder', marginTop: '1rem' }} data-testid="foo" />;
10 changes: 10 additions & 0 deletions packages/pigment-css-react/tests/Hidden.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from 'react';
import Hidden from '../src/Hidden';

<Hidden xsUp mdDown />;

<Hidden
className=""
style={{ accentColor: 'ActiveBorder', marginTop: '1rem' }}
data-testid="foo"
/>;
5 changes: 5 additions & 0 deletions packages/pigment-css-react/tests/Stack.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as React from 'react';
import Stack from '../src/Stack';

<Stack spacing={2} direction={{ md: 'row' }} />;

<Stack className="" style={{ accentColor: 'ActiveBorder', marginTop: '1rem' }} data-testid="foo" />;

<Stack
// @ts-expect-error
display={{
xs: 'flex',
xl: 'inline-flex',
Expand Down
17 changes: 17 additions & 0 deletions packages/pigment-css-react/tests/sx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SxProp } from '../src/sx';

const sx1: SxProp = { color: 'red' };

const sx2: SxProp = (theme) => ({
color: theme.palette.primary.main,
});

const sx3: SxProp = [{ color: 'red' }, { backgroundColor: 'blue', color: 'white' }];

const test = true;
const sx4: SxProp = [
test ? { color: 'red' } : { backgroundColor: 'blue', color: 'white' },
(theme) => ({
border: `1px solid ${theme.palette.primary.main}`,
}),
];

0 comments on commit 4576df1

Please sign in to comment.