This repository was archived by the owner on Apr 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: support custom rehydratable query selectors #49
Draft
chrisvxd
wants to merge
1
commit into
chrisvxd/remove-markup-containers
Choose a base branch
from
chrisvxd/custom-query-selectors
base: chrisvxd/remove-markup-containers
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| interface IOptions { | ||
| allSelectors: { [key: string]: string }; | ||
| compoundSelector: string; | ||
| extra: object; | ||
| } | ||
|
|
||
| export default IOptions; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| interface IOptions { | ||
| extra: object; | ||
| getQuerySelector?: (key: string) => string; | ||
| } | ||
|
|
||
| export default IOptions; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,24 @@ | ||
| import domElementToReact from "dom-element-to-react"; | ||
| import * as ReactDOM from "react-dom"; | ||
|
|
||
| import ILoadedOptions from "./ILoadedOptions"; | ||
| import IOptions from "./IOptions"; | ||
| import IRehydrator from "./IRehydrator"; | ||
|
|
||
| const rehydratableToReactElement = async ( | ||
| el: Element, | ||
| rehydrators: IRehydrator, | ||
| options: IOptions | ||
| options: ILoadedOptions | ||
| ): Promise<React.ReactElement<any>> => { | ||
| const rehydratorName = el.getAttribute("data-rehydratable"); | ||
| const rehydratorSelector = Object.keys(options.allSelectors).find(selector => | ||
| el.matches(selector) | ||
| ); | ||
|
|
||
| if (!rehydratorSelector) { | ||
| throw new Error("No rehydrator selector matched the element."); | ||
| } | ||
|
|
||
| const rehydratorName = options.allSelectors[rehydratorSelector]; | ||
|
|
||
| if (!rehydratorName) { | ||
| throw new Error("Rehydrator name is missing from element."); | ||
|
|
@@ -31,13 +40,13 @@ const rehydratableToReactElement = async ( | |
|
|
||
| const createCustomHandler = ( | ||
| rehydrators: IRehydrator, | ||
| options: IOptions | ||
| options: ILoadedOptions | ||
| ) => async (node: Node) => { | ||
| // This function will run on _every_ node that domElementToReact encounters. | ||
| // Make sure to keep the conditional highly performant. | ||
| if ( | ||
| node.nodeType === Node.ELEMENT_NODE && | ||
| (node as Element).hasAttribute("data-rehydratable") | ||
| (node as Element).matches(options.compoundSelector) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance consideration: How does |
||
| ) { | ||
| return rehydratableToReactElement(node as Element, rehydrators, options); | ||
| } | ||
|
|
@@ -61,7 +70,7 @@ const createReactRoot = (el: Node) => { | |
| const rehydrateChildren = async ( | ||
| el: Node, | ||
| rehydrators: IRehydrator, | ||
| options: IOptions | ||
| options: ILoadedOptions | ||
| ) => { | ||
| const container = createReactRoot(el); | ||
|
|
||
|
|
@@ -91,30 +100,55 @@ const render = ({ | |
| ReactDOM.render(rehydrated as React.ReactElement<any>, root); | ||
| }; | ||
|
|
||
| const createQuerySelector = (rehydratableIds: string[]) => | ||
| rehydratableIds.reduce( | ||
| (acc: string, rehydratableId: string) => | ||
| `${acc ? `${acc}, ` : ""}[data-rehydratable*="${rehydratableId}"]`, | ||
| const defaultGetQuerySelector = (key: string) => | ||
| `[data-rehydratable*="${key}"]`; | ||
|
|
||
| const createQuerySelectors = ( | ||
| rehydratableIds: string[], | ||
| getQuerySelector: ((key: string) => string) = defaultGetQuerySelector | ||
| ) => { | ||
| const allSelectors: { [key: string]: string } = rehydratableIds.reduce( | ||
| (acc, key) => ({ ...acc, [getQuerySelector(key)]: key }), | ||
| {} | ||
| ); | ||
|
|
||
| const compoundSelector = Object.keys(allSelectors).reduce( | ||
| (acc: string, selector: string) => `${acc ? `${acc}, ` : ""}${selector}`, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance consideration: Additional loop on each |
||
| "" | ||
| ); | ||
|
|
||
| return { | ||
| allSelectors, | ||
| compoundSelector | ||
| }; | ||
| }; | ||
|
|
||
| export default async ( | ||
| container: Element, | ||
| rehydrators: IRehydrator, | ||
| options: IOptions | ||
| ) => { | ||
| const selector = createQuerySelector(Object.keys(rehydrators)); | ||
|
|
||
| const roots = Array.from( | ||
| // TODO: allow setting a container identifier so multiple rehydration instances can exist | ||
| container.querySelectorAll(selector) | ||
| ).reduce((acc: Element[], root: Element) => { | ||
| // filter roots that are contained within other roots | ||
| if (!acc.some(r => r.contains(root))) { | ||
| acc.push(root); | ||
| } | ||
| return acc; | ||
| }, []); | ||
| const { allSelectors, compoundSelector } = createQuerySelectors( | ||
| Object.keys(rehydrators), | ||
| options.getQuerySelector | ||
| ); | ||
|
|
||
| const loadedOptions: ILoadedOptions = { | ||
| allSelectors, | ||
| compoundSelector, | ||
| extra: options.extra | ||
| }; | ||
|
|
||
| const roots = Array.from(container.querySelectorAll(compoundSelector)).reduce( | ||
| (acc: Element[], root: Element) => { | ||
| // filter roots that are contained within other roots | ||
| if (!acc.some(r => r.contains(root))) { | ||
| acc.push(root); | ||
| } | ||
| return acc; | ||
| }, | ||
| [] | ||
| ); | ||
|
|
||
| // TODO: solve race condition when a second rehydrate runs | ||
|
|
||
|
|
@@ -128,7 +162,7 @@ export default async ( | |
| const { | ||
| container: rootContainer, | ||
| rehydrated | ||
| } = await rehydrateChildren(root, rehydrators, options); | ||
| } = await rehydrateChildren(root, rehydrators, loadedOptions); | ||
|
|
||
| return { root: rootContainer, rehydrated }; | ||
| } catch (e) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance consideration: This introduces a
findloop on each call ofrehydratableToReactElement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth considering switch to for loop:
Source. Need more data.