forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Exp flyout expandable widget rendering (elastic#1…
…60625) ## Summary This tiny PR ensures that widget contents are not rendered until expanded (in the expandable flyout). This will prevent unnecessary requests being sent when we open the flyout. Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
bc4ffb6
commit 901b9eb
Showing
4 changed files
with
67 additions
and
13 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
48 changes: 48 additions & 0 deletions
48
x-pack/plugins/security_solution/public/flyout/right/hooks/use_accordion_state.ts
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,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useReducer } from 'react'; | ||
|
||
const CLOSED = 'closed' as const; | ||
const OPEN = 'open' as const; | ||
|
||
type ToggleReducerState = typeof CLOSED | typeof OPEN; | ||
const toggleReducer = (state: ToggleReducerState) => { | ||
return state === CLOSED ? OPEN : CLOSED; | ||
}; | ||
|
||
export interface UseAccordionStateValue { | ||
/** | ||
* Should children be rendered in the dom | ||
*/ | ||
renderContent: boolean; | ||
/** | ||
* Use this to control the accordion visual state | ||
*/ | ||
state: typeof CLOSED | typeof OPEN; | ||
|
||
/** | ||
* Handler function for cycling between the states | ||
*/ | ||
toggle: VoidFunction; | ||
} | ||
|
||
/** | ||
* Tiny hook for controlled useAccordionState | ||
* @param expandedInitially - is accordion expanded on first render | ||
*/ | ||
export const useAccordionState = (expandedInitially: boolean): UseAccordionStateValue => { | ||
const initialState = expandedInitially ? OPEN : CLOSED; | ||
const [state, toggle] = useReducer(toggleReducer, initialState); | ||
const renderContent = state === OPEN; | ||
|
||
return { | ||
renderContent, | ||
state, | ||
toggle, | ||
}; | ||
}; |