-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
KibanaThemeProvider
for security/spaces apps (#119124)
- Loading branch information
Showing
39 changed files
with
532 additions
and
218 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
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,9 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { KibanaThemeProvider } from './kibana_theme_provider'; |
88 changes: 88 additions & 0 deletions
88
src/plugins/interactive_setup/public/theme/kibana_theme_provider.test.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,88 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { useEuiTheme } from '@elastic/eui'; | ||
import type { ReactWrapper } from 'enzyme'; | ||
import type { FC } from 'react'; | ||
import React, { useEffect } from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { BehaviorSubject, of } from 'rxjs'; | ||
|
||
import { mountWithIntl } from '@kbn/test/jest'; | ||
import type { CoreTheme } from 'src/core/public'; | ||
|
||
import { KibanaThemeProvider } from './kibana_theme_provider'; | ||
|
||
describe('KibanaThemeProvider', () => { | ||
let euiTheme: ReturnType<typeof useEuiTheme> | undefined; | ||
|
||
beforeEach(() => { | ||
euiTheme = undefined; | ||
}); | ||
|
||
const flushPromises = async () => { | ||
await new Promise<void>(async (resolve, reject) => { | ||
try { | ||
setImmediate(() => resolve()); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
}; | ||
|
||
const InnerComponent: FC = () => { | ||
const theme = useEuiTheme(); | ||
useEffect(() => { | ||
euiTheme = theme; | ||
}, [theme]); | ||
return <div>foo</div>; | ||
}; | ||
|
||
const refresh = async (wrapper: ReactWrapper<unknown>) => { | ||
await act(async () => { | ||
await flushPromises(); | ||
wrapper.update(); | ||
}); | ||
}; | ||
|
||
it('exposes the EUI theme provider', async () => { | ||
const coreTheme: CoreTheme = { darkMode: true }; | ||
|
||
const wrapper = mountWithIntl( | ||
<KibanaThemeProvider theme$={of(coreTheme)}> | ||
<InnerComponent /> | ||
</KibanaThemeProvider> | ||
); | ||
|
||
await refresh(wrapper); | ||
|
||
expect(euiTheme!.colorMode).toEqual('DARK'); | ||
}); | ||
|
||
it('propagates changes of the coreTheme observable', async () => { | ||
const coreTheme$ = new BehaviorSubject<CoreTheme>({ darkMode: true }); | ||
|
||
const wrapper = mountWithIntl( | ||
<KibanaThemeProvider theme$={coreTheme$}> | ||
<InnerComponent /> | ||
</KibanaThemeProvider> | ||
); | ||
|
||
await refresh(wrapper); | ||
|
||
expect(euiTheme!.colorMode).toEqual('DARK'); | ||
|
||
await act(async () => { | ||
coreTheme$.next({ darkMode: false }); | ||
}); | ||
|
||
await refresh(wrapper); | ||
|
||
expect(euiTheme!.colorMode).toEqual('LIGHT'); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
src/plugins/interactive_setup/public/theme/kibana_theme_provider.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,33 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { EuiThemeProvider } from '@elastic/eui'; | ||
import type { FC } from 'react'; | ||
import React, { useMemo } from 'react'; | ||
import useObservable from 'react-use/lib/useObservable'; | ||
import type { Observable } from 'rxjs'; | ||
|
||
import type { CoreTheme } from '../../../../core/public'; | ||
import { getColorMode } from './utils'; | ||
|
||
interface KibanaThemeProviderProps { | ||
theme$: Observable<CoreTheme>; | ||
} | ||
|
||
const defaultTheme: CoreTheme = { | ||
darkMode: false, | ||
}; | ||
|
||
/** | ||
* Copied from the `kibana_react` plugin, remove once https://github.com/elastic/kibana/issues/119204 is implemented. | ||
*/ | ||
export const KibanaThemeProvider: FC<KibanaThemeProviderProps> = ({ theme$, children }) => { | ||
const theme = useObservable(theme$, defaultTheme); | ||
const colorMode = useMemo(() => getColorMode(theme), [theme]); | ||
return <EuiThemeProvider colorMode={colorMode}>{children}</EuiThemeProvider>; | ||
}; |
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,19 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getColorMode } from './utils'; | ||
|
||
describe('getColorMode', () => { | ||
it('returns the correct `colorMode` when `darkMode` is enabled', () => { | ||
expect(getColorMode({ darkMode: true })).toEqual('DARK'); | ||
}); | ||
|
||
it('returns the correct `colorMode` when `darkMode` is disabled', () => { | ||
expect(getColorMode({ darkMode: false })).toEqual('LIGHT'); | ||
}); | ||
}); |
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,19 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { EuiThemeColorMode } from '@elastic/eui/src/services/theme/types'; | ||
|
||
import type { CoreTheme } from '../../../../core/public'; | ||
|
||
/** | ||
* Copied from the `kibana_react` plugin, remove once https://github.com/elastic/kibana/issues/119204 is implemented. | ||
*/ | ||
export const getColorMode = (theme: CoreTheme): EuiThemeColorMode => { | ||
// COLOR_MODES_STANDARD is not exported from eui | ||
return theme.darkMode ? 'DARK' : 'LIGHT'; | ||
}; |
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.