-
-
Notifications
You must be signed in to change notification settings - Fork 733
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduce a highlight reusable component
- Loading branch information
Showing
11 changed files
with
248 additions
and
144 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
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
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
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,43 @@ | ||
import { alpha, styled } from '@mui/material'; | ||
import type { ReactNode } from 'react'; | ||
import { useHighlightContext } from './HighlightContext'; | ||
import type { HighlightKey } from './HighlightProvider'; | ||
|
||
const StyledHighlight = styled('div', { | ||
shouldForwardProp: (prop) => prop !== 'highlighted', | ||
})<{ highlighted: boolean }>(({ theme, highlighted }) => ({ | ||
'&&& > *': { | ||
animation: highlighted ? 'pulse 1.5s infinite linear' : 'none', | ||
zIndex: highlighted ? theme.zIndex.tooltip : 'auto', | ||
transition: 'box-shadow 0.3s ease', | ||
'@keyframes pulse': { | ||
'0%': { | ||
boxShadow: `0 0 0 0px ${alpha(theme.palette.primary.main, 0.5)}`, | ||
transform: 'scale(1)', | ||
}, | ||
'50%': { | ||
boxShadow: `0 0 0 15px ${alpha(theme.palette.primary.main, 0.2)}`, | ||
transform: 'scale(1.05)', | ||
}, | ||
'100%': { | ||
boxShadow: `0 0 0 30px ${alpha(theme.palette.primary.main, 0)}`, | ||
transform: 'scale(1)', | ||
}, | ||
}, | ||
}, | ||
})); | ||
|
||
interface IHighlightProps { | ||
highlightKey: HighlightKey; | ||
children: ReactNode; | ||
} | ||
|
||
export const Highlight = ({ highlightKey, children }: IHighlightProps) => { | ||
const { isHighlighted } = useHighlightContext(); | ||
|
||
return ( | ||
<StyledHighlight highlighted={isHighlighted(highlightKey)}> | ||
{children} | ||
</StyledHighlight> | ||
); | ||
}; |
18 changes: 18 additions & 0 deletions
18
frontend/src/component/common/Highlight/HighlightContext.tsx
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,18 @@ | ||
import { createContext, useContext } from 'react'; | ||
import type { IHighlightContext } from './HighlightProvider'; | ||
|
||
export const HighlightContext = createContext<IHighlightContext | undefined>( | ||
undefined, | ||
); | ||
|
||
export const useHighlightContext = (): IHighlightContext => { | ||
const context = useContext(HighlightContext); | ||
|
||
if (!context) { | ||
throw new Error( | ||
'useHighlightContext must be used within a HighlightProvider', | ||
); | ||
} | ||
|
||
return context; | ||
}; |
45 changes: 45 additions & 0 deletions
45
frontend/src/component/common/Highlight/HighlightProvider.tsx
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,45 @@ | ||
import { useState, type ReactNode } from 'react'; | ||
import { HighlightContext } from './HighlightContext'; | ||
|
||
const defaultState = { | ||
eventTimeline: false, | ||
unleashAI: false, | ||
}; | ||
|
||
export type HighlightKey = keyof typeof defaultState; | ||
type HighlightState = typeof defaultState; | ||
|
||
export interface IHighlightContext { | ||
isHighlighted: (key: HighlightKey) => boolean; | ||
highlight: (key: HighlightKey, timeout?: number) => void; | ||
} | ||
|
||
interface IHighlightProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const HighlightProvider = ({ children }: IHighlightProviderProps) => { | ||
const [state, setState] = useState<HighlightState>(defaultState); | ||
|
||
const isHighlighted = (key: HighlightKey) => state[key]; | ||
|
||
const setHighlight = (key: HighlightKey, value: boolean) => { | ||
setState((prevState) => ({ ...prevState, [key]: value })); | ||
}; | ||
|
||
const highlight = (key: HighlightKey, timeout = 3000) => { | ||
setHighlight(key, true); | ||
setTimeout(() => setHighlight(key, false), timeout); | ||
}; | ||
|
||
const contextValue: IHighlightContext = { | ||
isHighlighted, | ||
highlight, | ||
}; | ||
|
||
return ( | ||
<HighlightContext.Provider value={contextValue}> | ||
{children} | ||
</HighlightContext.Provider> | ||
); | ||
}; |
Oops, something went wrong.