-
-
Notifications
You must be signed in to change notification settings - Fork 530
docs: Added package manager tab group synchronization across the entire installation page #2421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BlazerYoo
wants to merge
7
commits into
wxt-dev:main
Choose a base branch
from
BlazerYoo:main
base: main
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.
+76
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c6c3c8
fix(docs): sync code group tabs across installation page
BlazerYoo 444b9ed
Apply suggestions from code review
BlazerYoo 077aaf6
Merge branch 'main' into main
BlazerYoo 5e5cdd9
refactor(docs): extract code group sync into utils helper
BlazerYoo 3fa4acc
Merge branch 'wxt-dev:main' into main
BlazerYoo 633c049
rename code group sync utils helper
BlazerYoo 86f203c
Merge branch 'wxt-dev:main' into main
BlazerYoo 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
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,73 @@ | ||
| const CODE_GROUP_SELECTORS = { | ||
| root: '.vp-code-group', | ||
| input: '.vp-code-group .tabs input', | ||
| label: '.tabs label', | ||
| } as const; | ||
|
|
||
| function getTabTitle(label: HTMLLabelElement) { | ||
| return label.dataset.title?.trim() || label.textContent?.trim(); | ||
| } | ||
|
|
||
| function getCodeGroupTitle(input: HTMLInputElement) { | ||
| const label = input.parentElement?.querySelector<HTMLLabelElement>( | ||
| `label[for="${input.id}"]`, | ||
| ); | ||
|
|
||
| return label ? getTabTitle(label) : undefined; | ||
| } | ||
|
|
||
| function findCodeGroupTabIndex(group: HTMLElement, title: string) { | ||
| return Array.from( | ||
| group.querySelectorAll<HTMLLabelElement>(CODE_GROUP_SELECTORS.label), | ||
| ).findIndex((label) => getTabTitle(label) === title); | ||
| } | ||
|
|
||
| function syncCodeGroupSelection(group: HTMLElement, title: string) { | ||
| const targetIndex = findCodeGroupTabIndex(group, title); | ||
|
|
||
| if (targetIndex < 0) return; | ||
|
|
||
| const blocks = group.querySelector('.blocks'); | ||
| const targetBlock = blocks?.children[targetIndex]; | ||
| if (!blocks || !targetBlock || targetBlock.classList.contains('active')) | ||
| return; | ||
|
|
||
| blocks.querySelector('.active')?.classList.remove('active'); | ||
| targetBlock.classList.add('active'); | ||
|
|
||
| const input = group.querySelectorAll<HTMLInputElement>( | ||
| CODE_GROUP_SELECTORS.input, | ||
| )[targetIndex]; | ||
| if (input) input.checked = true; | ||
| } | ||
|
|
||
| function syncCodeGroupTabs(event: Event) { | ||
| const target = event.target; | ||
| if ( | ||
| !(target instanceof HTMLInputElement) || | ||
| !target.matches(CODE_GROUP_SELECTORS.input) | ||
| ) | ||
| return; | ||
|
|
||
| const activeGroup = target.closest(CODE_GROUP_SELECTORS.root); | ||
| const activeTitle = getCodeGroupTitle(target); | ||
| if (!activeTitle) return; | ||
|
|
||
| document | ||
| .querySelectorAll<HTMLElement>(CODE_GROUP_SELECTORS.root) | ||
| .forEach((group) => { | ||
| if (group === activeGroup) return; | ||
|
|
||
| syncCodeGroupSelection(group, activeTitle); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Keeps every code group on the page in sync: selecting a package manager (or | ||
| * any tab) in one group switches the matching tab in all the other groups. | ||
| */ | ||
| export function setupCodeGroupSync() { | ||
| if (typeof window === 'undefined') return; | ||
|
|
||
| window.addEventListener('change', syncCodeGroupTabs, true); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.