Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added transient config option #7

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/docs/add-variants.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,50 @@ type CheckboxProps = W.infer<typeof Checkbox>;
// CheckboxProps['color'] is 'gray' | 'red' | undefined
```

## Transient Variants

Sometimes you want to pass a variant that is not a custom component prop. You can do that with `transient` option. This is useful if you want a pass a variable only for styling purposes to variant.

```typescript
import { w, W } from 'windstitch';

const CustomComponent: React.FC<{
className: string;
active?: boolean;
}> = props => <p {...props}>{props.active ? "I'm active" : "I'm inactive"}</p>;

const StyledComponent = w(CustomComponent, {
variants: {
active: (yes: boolean) => (yes ? 'text-indigo-500' : 'text-white'),
},
transient: ['active'],
});

<StyledComponent active={true} />;
// Renders <p class="text-indigo-500">I'm inactive</p>
```

If `transient` options is not set, the variant will be passed to the component as a prop, sometimes resulting in an HTML error.

```typescript
import { w, W } from 'windstitch';

const CustomComponent: React.FC<{
className: string;
active?: boolean;
}> = props => <p {...props}>{props.active ? "I'm active" : "I'm inactive"}</p>;

const StyledComponent = w(CustomComponent, {
variants: {
active: (yes: boolean) => (yes ? 'text-indigo-500' : 'text-white'),
},
});

<StyledComponent active={true} />;
// Renders <p class="text-indigo-500">I'm active</p>,
// but gives a warning: "Received `true` for a non-boolean attribute `active`" in the console
```

## Reusing styles

As prop types are inferred by your variants shape, you can easily reuse styles across components.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.2.0",
"version": "0.2.1",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
18 changes: 16 additions & 2 deletions src/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@ import { evaluateClassName } from './utils';

export const styled: W.Styled = function(
component,
{ className: defaultClassName, variants, defaultProps, defaultVariants }
{
className: defaultClassName,
variants,
transient,
defaultProps,
defaultVariants,
}
) {
const overrideVariantProps = variants
? Object.fromEntries(Object.keys(variants).map(key => [key, undefined]))
: {};
const overrideTransientProps =
transient && variants
? Object.fromEntries(
Object.keys(variants)
.filter(key => transient.includes(key))
.map(key => [key, undefined])
)
: {};
const Component = <As extends ElementType>(
{ as: asProp, ...props }: W.StyledProps<As, any, any>,
ref: any
Expand All @@ -22,7 +36,7 @@ export const styled: W.Styled = function(
{...defaultProps}
{...props}
// remove all variants props if the component is a tag name
{...(isTag ? overrideVariantProps : {})}
{...(isTag ? overrideVariantProps : overrideTransientProps)}
ref={isTag ? ref : undefined}
className={
evaluateClassName(
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface ComponentConfig<
> {
className?: string;
variants?: Variants;
transient?: (keyof Variants)[];
defaultVariants?: DefaultVariants;
defaultProps?: Partial<
InferAnyComponentProps<ToIntrinsicElementIfPossible<DefaultAs>>
Expand Down