diff --git a/package-lock.json b/package-lock.json index 9b3d863..092c79f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -137,6 +137,7 @@ "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.22.10.tgz", "integrity": "sha512-fTmqbbUBAwCcre6zPzNngvsI0aNrPZe77AeqvDxWM9Nm+04RrJ3CAmGHA9f7lJQY6ZMhRztNemy4uslDxTX4Qw==", "dev": true, + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.22.10", @@ -4083,6 +4084,7 @@ "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.4.1.tgz", "integrity": "sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==", "dev": true, + "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -4719,6 +4721,7 @@ "url": "https://github.com/sponsors/ai" } ], + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001517", "electron-to-chromium": "^1.4.477", @@ -5606,7 +5609,8 @@ "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.1475386.tgz", "integrity": "sha512-RQ809ykTfJ+dgj9bftdeL2vRVxASAuGU+I9LEx9Ij5TXU5HrgAQVmzi72VA+mkzscE12uzlRv5/tWWv9R9J1SA==", "dev": true, - "license": "BSD-3-Clause" + "license": "BSD-3-Clause", + "peer": true }, "node_modules/diff": { "version": "5.1.0", @@ -6062,6 +6066,7 @@ "resolved": "https://registry.npmjs.org/eslint/-/eslint-7.32.0.tgz", "integrity": "sha512-VHZ8gX+EDfz+97jGcgyGCyRia/dPOd6Xh9yPv8Bl1+SoaIwD+a/vlrOmGRUyOYu7MwUhc7CxqeaDZU13S4+EpA==", "dev": true, + "peer": true, "dependencies": { "@babel/code-frame": "7.12.11", "@eslint/eslintrc": "^0.4.3", @@ -9522,6 +9527,7 @@ "url": "https://github.com/sponsors/ai" } ], + "peer": true, "dependencies": { "nanoid": "^3.3.6", "picocolors": "^1.0.0", @@ -10595,6 +10601,7 @@ "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.79.1.tgz", "integrity": "sha512-uKxbd0IhMZOhjAiD5oAFp7BqvkA4Dv47qpOCtaNvng4HBwdbWtdOh8f5nZNuk2rp51PMGk3bzfWu5oayNEuYnw==", "dev": true, + "peer": true, "bin": { "rollup": "dist/bin/rollup" }, @@ -11915,6 +11922,7 @@ "integrity": "sha512-CWBzXQrc/qOkhidw1OzBTQuYRbfyxDXJMVJ1XNwUHGROVmuaeiEm3OslpZ1RV96d7SKKjZKrSJu3+t/xlw3R9A==", "dev": true, "license": "Apache-2.0", + "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/src/index.js b/src/index.js index 1ae5828..c38ef48 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import { h, cloneElement, render, hydrate, Fragment } from 'preact'; +import { h, cloneElement, render, hydrate } from 'preact'; /** * @typedef {import('./internal.d.ts').PreactCustomElement} PreactCustomElement @@ -177,8 +177,7 @@ function Slot(props, context) { } } }; - const { useFragment, ...rest } = props; - return h(useFragment ? Fragment : 'slot', { ...rest, ref }); + return h('slot', { ...props, ref }); } function toVdom(element, nodeName, options) { @@ -210,9 +209,8 @@ function toVdom(element, nodeName, options) { const shadow = !!(options && options.shadow); // Only wrap the topmost node with a slot - const wrappedChildren = nodeName - ? h(Slot, { useFragment: !shadow }, children) - : children; + const wrappedChildren = + nodeName && shadow ? h(Slot, null, children) : children; if (!shadow && nodeName) { element.innerHTML = ''; diff --git a/test/index.test.jsx b/test/index.test.jsx index 734ab6c..fe2a78b 100644 --- a/test/index.test.jsx +++ b/test/index.test.jsx @@ -285,6 +285,46 @@ describe('web components', () => { assert.equal(getShadowHTML(), '

Active theme: sunny

'); }); + it.only('passes context over light DOM custom element boundaries', async () => { + const Theme = createContext('light'); + + function DisplayTheme() { + const theme = useContext(Theme); + return

Active theme: {theme}

; + } + + function Parent({ children, theme = 'dark' }) { + return ( + +
{children}
+
+ ); + } + + registerElement(Parent, 'x-light-dom-parent', ['theme']); + registerElement(DisplayTheme, 'x-light-dom-child', []); + + const el = document.createElement('x-light-dom-parent'); + + const noSlot = document.createElement('x-light-dom-child'); + el.appendChild(noSlot); + + root.appendChild(el); + assert.equal( + root.innerHTML, + '

Active theme: dark

' + ); + + // Trigger context update + act(() => { + el.setAttribute('theme', 'sunny'); + }); + assert.equal( + root.innerHTML, + '

Active theme: sunny

' + ); + }); + it('renders element in shadow dom open mode', async () => { function ShadowDomOpen() { return
Shadow DOM Open
;