Skip to content

Commit

Permalink
Change domPassthrough behaviour (now just passthrough) (#25)
Browse files Browse the repository at this point in the history
* Change domPassthrough behaviour

* Version bump
  • Loading branch information
John Richard Chipps-Harding authored Feb 14, 2023
1 parent 9fced48 commit 5be9414
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ const Link = styled(NextLink, {
});
```

### DOM Shielding
### Passthrough

By default variant values do not end up propagating to the final DOM element. This is to stop React specific runtime errors from occurring. If you do indeed want to pass a variant value to the DOM element, you can use the `domPassthrough` option.
By default variant values do not end up propagating to the element it is extending and is exclusively used for styling the current component. This is to stop React specific runtime errors from occurring with regards to the DOM. If you do indeed want to pass a variant value to the element you are extending, you can use the `passthrough` option.

In the following example, `readOnly` is an intrinsic HTML attribute that we both want to style, but also continue to pass through to the DOM element.

Expand All @@ -233,7 +233,7 @@ const Input = styled("input", {
true: css.disabledStyle,
},
},
domPassthrough: ["readOnly"],
passthrough: ["readOnly"],
});
```

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@phntms/css-components",
"description": "At its core, css-components is a simple wrapper around standard CSS. It allows you to write your CSS how you wish then compose them into a component ready to be used in React.",
"version": "0.2.0",
"version": "0.3.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"homepage": "https://github.com/phantomstudios/css-components#readme",
Expand Down
8 changes: 1 addition & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,11 @@ export const styled = <
}
}

const isDomNode = typeof element === "string";
const isVariant =
config?.variants && config.variants.hasOwnProperty(key);

// Only pass through the prop if it's not a variant or been told to pass through
if (
isDomNode &&
isVariant &&
!config?.domPassthrough?.includes(key as keyof V)
)
return;
if (isVariant && !config?.passthrough?.includes(key as keyof V)) return;

componentProps[key] = mergedProps[key];
});
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface CSSComponentConfig<V> {
defaultVariants?: {
[Property in keyof V]?: BooleanIfStringBoolean<keyof V[Property]>;
};
domPassthrough?: (keyof V)[];
passthrough?: (keyof V)[];
}

/**
Expand Down
14 changes: 12 additions & 2 deletions test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ describe("Basic functionality", () => {
expect(container.firstChild).toHaveClass("summary");
});

it("should pass through classNames for composed css-components", async () => {
const BaseParagraph = styled("p", { css: "baseParagraph" });
const Paragraph = styled(BaseParagraph, { css: "paragraph" });
const { container } = render(<Paragraph>Hello</Paragraph>);

expect(container.firstChild).toHaveClass("baseParagraph");
expect(container.firstChild).toHaveClass("paragraph");
});

it("should pass through multiple children", async () => {
const Article = styled("article");
const { container } = render(
Expand Down Expand Up @@ -328,6 +337,7 @@ describe("supports inheritance", () => {
variants: {
big: { true: "checkoutButtonBig" },
},
passthrough: ["big"],
});

const { container } = render(<CheckoutButton big />);
Expand Down Expand Up @@ -416,7 +426,7 @@ describe("supports inheritance", () => {
variants: {
type: { text: "textInput" },
},
domPassthrough: ["type"],
passthrough: ["type"],
});

const { container } = render(<Input type="text" />);
Expand All @@ -432,7 +442,7 @@ describe("supports inheritance", () => {
variants: {
readOnly: { true: "readOnly" },
},
domPassthrough: ["readOnly"],
passthrough: ["readOnly"],
});

const { container } = render(<Input type="text" readOnly />);
Expand Down

0 comments on commit 5be9414

Please sign in to comment.