Skip to content

Commit 5be9414

Browse files
author
John Richard Chipps-Harding
authored
Change domPassthrough behaviour (now just passthrough) (#25)
* Change domPassthrough behaviour * Version bump
1 parent 9fced48 commit 5be9414

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ const Link = styled(NextLink, {
216216
});
217217
```
218218

219-
### DOM Shielding
219+
### Passthrough
220220

221-
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.
221+
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.
222222

223223
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.
224224

@@ -233,7 +233,7 @@ const Input = styled("input", {
233233
true: css.disabledStyle,
234234
},
235235
},
236-
domPassthrough: ["readOnly"],
236+
passthrough: ["readOnly"],
237237
});
238238
```
239239

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@phntms/css-components",
33
"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.",
4-
"version": "0.2.0",
4+
"version": "0.3.0",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
77
"homepage": "https://github.com/phantomstudios/css-components#readme",

src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,11 @@ export const styled = <
4848
}
4949
}
5050

51-
const isDomNode = typeof element === "string";
5251
const isVariant =
5352
config?.variants && config.variants.hasOwnProperty(key);
5453

5554
// Only pass through the prop if it's not a variant or been told to pass through
56-
if (
57-
isDomNode &&
58-
isVariant &&
59-
!config?.domPassthrough?.includes(key as keyof V)
60-
)
61-
return;
55+
if (isVariant && !config?.passthrough?.includes(key as keyof V)) return;
6256

6357
componentProps[key] = mergedProps[key];
6458
});

src/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface CSSComponentConfig<V> {
6767
defaultVariants?: {
6868
[Property in keyof V]?: BooleanIfStringBoolean<keyof V[Property]>;
6969
};
70-
domPassthrough?: (keyof V)[];
70+
passthrough?: (keyof V)[];
7171
}
7272

7373
/**

test/index.test.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ describe("Basic functionality", () => {
3737
expect(container.firstChild).toHaveClass("summary");
3838
});
3939

40+
it("should pass through classNames for composed css-components", async () => {
41+
const BaseParagraph = styled("p", { css: "baseParagraph" });
42+
const Paragraph = styled(BaseParagraph, { css: "paragraph" });
43+
const { container } = render(<Paragraph>Hello</Paragraph>);
44+
45+
expect(container.firstChild).toHaveClass("baseParagraph");
46+
expect(container.firstChild).toHaveClass("paragraph");
47+
});
48+
4049
it("should pass through multiple children", async () => {
4150
const Article = styled("article");
4251
const { container } = render(
@@ -328,6 +337,7 @@ describe("supports inheritance", () => {
328337
variants: {
329338
big: { true: "checkoutButtonBig" },
330339
},
340+
passthrough: ["big"],
331341
});
332342

333343
const { container } = render(<CheckoutButton big />);
@@ -416,7 +426,7 @@ describe("supports inheritance", () => {
416426
variants: {
417427
type: { text: "textInput" },
418428
},
419-
domPassthrough: ["type"],
429+
passthrough: ["type"],
420430
});
421431

422432
const { container } = render(<Input type="text" />);
@@ -432,7 +442,7 @@ describe("supports inheritance", () => {
432442
variants: {
433443
readOnly: { true: "readOnly" },
434444
},
435-
domPassthrough: ["readOnly"],
445+
passthrough: ["readOnly"],
436446
});
437447

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

0 commit comments

Comments
 (0)