| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import * as React from 'react'; |
| 11 | import {useContext, useMemo} from 'react'; |
| 12 | |
| 13 | import {StoreContext} from 'react-devtools-shared/src/devtools/views/context'; |
| 14 | import {useSubscription} from 'react-devtools-shared/src/devtools/views/hooks'; |
| 15 | import {TreeStateContext} from 'react-devtools-shared/src/devtools/views/Components/TreeContext'; |
| 16 | |
| 17 | import {NativeStyleContext} from './context'; |
| 18 | import LayoutViewer from './LayoutViewer'; |
| 19 | import StyleEditor from './StyleEditor'; |
| 20 | import styles from './index.css'; |
| 21 | |
| 22 | export default function NativeStyleEditorWrapper(): React.Node { |
| 23 | const store = useContext(StoreContext); |
| 24 | const subscription = useMemo( |
| 25 | () => ({ |
| 26 | getCurrentValue: () => store.supportsNativeStyleEditor, |
| 27 | subscribe: (callback: Function) => { |
| 28 | store.addListener('supportsNativeStyleEditor', callback); |
| 29 | return () => { |
| 30 | store.removeListener('supportsNativeStyleEditor', callback); |
| 31 | }; |
| 32 | }, |
| 33 | }), |
| 34 | [store], |
| 35 | ); |
| 36 | |
| 37 | const supportsNativeStyleEditor = useSubscription<boolean>(subscription); |
| 38 | if (!supportsNativeStyleEditor) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | return <NativeStyleEditor />; |
| 43 | } |
| 44 | |
| 45 | function NativeStyleEditor() { |
| 46 | const {inspectedElementID} = useContext(TreeStateContext); |
| 47 | const inspectedElementStyleAndLayout = useContext(NativeStyleContext); |
| 48 | if (inspectedElementID === null) { |
| 49 | return null; |
| 50 | } |
| 51 | if (inspectedElementStyleAndLayout === null) { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | const {layout, style} = inspectedElementStyleAndLayout; |
| 56 | if (layout === null && style === null) { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | return ( |
| 61 | <div className={styles.Stack}> |
| 62 | {layout !== null && ( |
| 63 | <LayoutViewer id={inspectedElementID} layout={layout} /> |
| 64 | )} |
| 65 | {style !== null && <StyleEditor id={inspectedElementID} style={style} />} |
| 66 | </div> |
| 67 | ); |
| 68 | } |