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.
[7.x] [Composable template] Preview composite template (elastic#72598) (
elastic#73191) Co-authored-by: Jean-Louis Leysens <jloleysens@gmail.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
d47ab97
commit 42b3a79
Showing
85 changed files
with
1,878 additions
and
986 deletions.
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
src/plugins/es_ui_shared/__packages_do_not_import__/global_flyout/global_flyout.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,166 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import React, { | ||
createContext, | ||
useContext, | ||
useState, | ||
useCallback, | ||
useMemo, | ||
useEffect, | ||
useRef, | ||
} from 'react'; | ||
import { EuiFlyout } from '@elastic/eui'; | ||
|
||
interface Context { | ||
addContent: <P extends object = { [key: string]: any }>(content: Content<P>) => void; | ||
removeContent: (contentId: string) => void; | ||
closeFlyout: () => void; | ||
} | ||
|
||
interface Content<P extends object = { [key: string]: any }> { | ||
id: string; | ||
Component: React.FunctionComponent<P>; | ||
props?: P; | ||
flyoutProps?: { [key: string]: any }; | ||
cleanUpFunc?: () => void; | ||
} | ||
|
||
const FlyoutMultiContentContext = createContext<Context | undefined>(undefined); | ||
|
||
const DEFAULT_FLYOUT_PROPS = { | ||
'data-test-subj': 'flyout', | ||
size: 'm' as 'm', | ||
maxWidth: 500, | ||
}; | ||
|
||
export const GlobalFlyoutProvider: React.FC = ({ children }) => { | ||
const [showFlyout, setShowFlyout] = useState(false); | ||
const [activeContent, setActiveContent] = useState<Content<any> | undefined>(undefined); | ||
|
||
const { id, Component, props, flyoutProps } = activeContent ?? {}; | ||
|
||
const addContent: Context['addContent'] = useCallback((content) => { | ||
setActiveContent((prev) => { | ||
if (prev !== undefined) { | ||
if (prev.id !== content.id && prev.cleanUpFunc) { | ||
// Clean up anything from the content about to be removed | ||
prev.cleanUpFunc(); | ||
} | ||
} | ||
return content; | ||
}); | ||
|
||
setShowFlyout(true); | ||
}, []); | ||
|
||
const closeFlyout: Context['closeFlyout'] = useCallback(() => { | ||
setActiveContent(undefined); | ||
setShowFlyout(false); | ||
}, []); | ||
|
||
const removeContent: Context['removeContent'] = useCallback( | ||
(contentId: string) => { | ||
if (contentId === id) { | ||
closeFlyout(); | ||
} | ||
}, | ||
[id, closeFlyout] | ||
); | ||
|
||
const mergedFlyoutProps = useMemo(() => { | ||
return { | ||
...DEFAULT_FLYOUT_PROPS, | ||
onClose: closeFlyout, | ||
...flyoutProps, | ||
}; | ||
}, [flyoutProps, closeFlyout]); | ||
|
||
const context: Context = { | ||
addContent, | ||
removeContent, | ||
closeFlyout, | ||
}; | ||
|
||
const ContentFlyout = showFlyout && Component !== undefined ? Component : null; | ||
|
||
return ( | ||
<FlyoutMultiContentContext.Provider value={context}> | ||
<> | ||
{children} | ||
{ContentFlyout && ( | ||
<EuiFlyout {...mergedFlyoutProps}> | ||
<ContentFlyout {...props} /> | ||
</EuiFlyout> | ||
)} | ||
</> | ||
</FlyoutMultiContentContext.Provider> | ||
); | ||
}; | ||
|
||
export const useGlobalFlyout = () => { | ||
const ctx = useContext(FlyoutMultiContentContext); | ||
|
||
if (ctx === undefined) { | ||
throw new Error('useGlobalFlyout must be used within a <GlobalFlyoutProvider />'); | ||
} | ||
|
||
const isMounted = useRef(false); | ||
/** | ||
* A component can add one or multiple content to the flyout | ||
* during its lifecycle. When it unmounts, we will remove | ||
* all those content added to the flyout. | ||
*/ | ||
const contents = useRef<Set<string> | undefined>(undefined); | ||
const { removeContent, addContent: addContentToContext } = ctx; | ||
|
||
useEffect(() => { | ||
isMounted.current = true; | ||
|
||
return () => { | ||
isMounted.current = false; | ||
}; | ||
}, []); | ||
|
||
const getContents = useCallback(() => { | ||
if (contents.current === undefined) { | ||
contents.current = new Set(); | ||
} | ||
return contents.current; | ||
}, []); | ||
|
||
const addContent: Context['addContent'] = useCallback( | ||
(content) => { | ||
getContents().add(content.id); | ||
return addContentToContext(content); | ||
}, | ||
[getContents, addContentToContext] | ||
); | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (!isMounted.current) { | ||
// When the component unmounts, remove all the content it has added to the flyout | ||
Array.from(getContents()).forEach(removeContent); | ||
} | ||
}; | ||
}, [removeContent]); | ||
|
||
return { ...ctx, addContent }; | ||
}; |
20 changes: 20 additions & 0 deletions
20
src/plugins/es_ui_shared/__packages_do_not_import__/global_flyout/index.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,20 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { GlobalFlyoutProvider, useGlobalFlyout } from './global_flyout'; |
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,23 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { | ||
GlobalFlyoutProvider, | ||
useGlobalFlyout, | ||
} from '../../__packages_do_not_import__/global_flyout'; |
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
Oops, something went wrong.