-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added selection feature to settings
- Loading branch information
Showing
8 changed files
with
338 additions
and
110 deletions.
There are no files selected for viewing
This file contains 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,33 @@ | ||
import React from 'react'; | ||
import {SelectedSettingsPart, SettingsPage, getSelectedSettingsPart} from '../collect-settings'; | ||
import {SettingsSelection} from './types'; | ||
|
||
interface ContextValue extends SelectedSettingsPart { | ||
selectedRef?: React.RefObject<HTMLDivElement>; | ||
} | ||
|
||
const defaultValue: ContextValue = {}; | ||
|
||
const context = React.createContext(defaultValue); | ||
context.displayName = 'SettingsSelectionContext'; | ||
|
||
export function useSettingsSelectionProviderValue( | ||
pages: Record<string, SettingsPage>, | ||
selection: SettingsSelection | undefined, | ||
): ContextValue { | ||
const selectedRef = React.useRef<HTMLDivElement>(null); | ||
|
||
const contextValue: ContextValue = React.useMemo(() => { | ||
if (!selection) return {selectedRef}; | ||
|
||
return {selectedRef, ...getSelectedSettingsPart(pages, selection)}; | ||
}, [pages, selection]); | ||
|
||
return contextValue; | ||
} | ||
|
||
export const SettingsSelectionContextProvider = context.Provider; | ||
|
||
export function useSettingsSelectionContext() { | ||
return React.useContext(context); | ||
} |
This file contains 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,2 @@ | ||
export * from './context'; | ||
export * from './types'; |
This file contains 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,5 @@ | ||
export interface SettingsSelection { | ||
page?: string; | ||
section?: {id: string} | {title: string}; | ||
settingId?: string; | ||
} |
This file contains 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,21 @@ | ||
import {SelectedSettingsPart, SettingsPageSection} from '../collect-settings'; | ||
|
||
export function isSectionSelected( | ||
selected: SelectedSettingsPart, | ||
pageId: string, | ||
section: SettingsPageSection, | ||
) { | ||
if (!selected.section || selected.setting) { | ||
return false; | ||
} else if (selected.section.id && selected.section.id === section.id) { | ||
return true; | ||
} else if ( | ||
selected.page?.id === pageId && | ||
selected.section.title && | ||
selected.section.title === section.title | ||
) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.