Skip to content

Commit

Permalink
fix: fix suppressHydrationWarning & track/source sync (#19)
Browse files Browse the repository at this point in the history
fix(build-react-wrapper): fix suppressHydrationWarning

fix(custom-media-element): fix track/source sync
  • Loading branch information
luwes authored May 24, 2024
1 parent dada41b commit 586d221
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 9 additions & 1 deletion examples/nextjs/app/hls-video/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ export default function Page() {
controls
crossOrigin=""
playsInline
></Player>
suppressHydrationWarning
>
<track
label="thumbnails"
default
kind="metadata"
src="https://image.mux.com/jtWZbHQ013SLyISc9LbIGn8f4c3lWan00qOkoPMZEXmcU/storyboard.vtt"
/>
</Player>
</section>
</>
);
Expand Down
4 changes: 3 additions & 1 deletion packages/custom-media-element/custom-media-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export const CustomMediaMixin = (superclass, { tag, is }) => {
}

get nativeEl() {
this.#init();
return this.#nativeEl
?? this.shadowRoot.querySelector(tag)
?? this.querySelector(':scope > [slot=media]')
Expand Down Expand Up @@ -280,6 +281,7 @@ export const CustomMediaMixin = (superclass, { tag, is }) => {
}

this.shadowRoot.addEventListener('slotchange', this);
this.#syncMediaChildren();

for (let type of this.constructor.Events) {
this.shadowRoot.addEventListener?.(type, this, true);
Expand Down Expand Up @@ -309,7 +311,7 @@ export const CustomMediaMixin = (superclass, { tag, is }) => {
const removeNativeChildren = new Map(this.#childMap);

this.shadowRoot.querySelector('slot:not([name])')
.assignedElements()
.assignedElements({ flatten: true })
.filter((el) => ['track', 'source'].includes(el.localName))
.forEach((el) => {
// If the source or track is still in the assigned elements keep it.
Expand Down
16 changes: 11 additions & 5 deletions scripts/build-react-wrapper/templates/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import React, { useRef, useEffect } from 'react';
import Element from '../{{{file_name}}}.js';

export default React.forwardRef(({ children, ...props }, ref) => {
export default React.forwardRef((allProps, ref) => {
let { children, suppressHydrationWarning, ...props } = allProps;
ref ??= useRef();

const attrs = propsToAttrs({ ...props, ref });

for (let name in props) {
if (name[0] === 'o' && name[1] === 'n') {
const useCapture = name.endsWith('Capture');
Expand All @@ -27,6 +26,8 @@ export default React.forwardRef(({ children, ...props }, ref) => {
}
}

const attrs = propsToAttrs(props);

// Only render the custom element template HTML on the server..
// The custom element will render itself on the client.
if (typeof window === 'undefined' && Element?.getTemplateHTML && Element?.shadowRootOptions) {
Expand All @@ -43,7 +44,12 @@ export default React.forwardRef(({ children, ...props }, ref) => {
children = [templateShadowRoot, children];
}

return React.createElement('{{{element_name}}}', attrs, ...[].concat(children));
return React.createElement('{{{element_name}}}', {
...attrs,
ref,
children,
suppressHydrationWarning,
});
});

const ReactPropToAttrNameMap = {
Expand All @@ -66,7 +72,7 @@ function toAttrName(propName, propValue) {
if (ReactPropToAttrNameMap[propName]) return ReactPropToAttrNameMap[propName];
if (typeof propValue == 'undefined') return undefined;
if (typeof propValue === 'boolean' && !propValue) return undefined;
if (/^on[A-Z]/.test(propName)) return undefined;
if (propName.startsWith('on') && typeof propValue === 'function') return undefined;
if (/[A-Z]/.test(propName)) return propName.toLowerCase();
return propName;
}
Expand Down

0 comments on commit 586d221

Please sign in to comment.