Skip to content

Commit 607a580

Browse files
author
Luke Bowerman
authored
Introduce ExtendComponentsProvider (#1636)
* Introduce ExtendComponentsProvider
1 parent 3093ee9 commit 607a580

File tree

5 files changed

+84
-17
lines changed

5 files changed

+84
-17
lines changed

packages/components-providers/src/ComponentsProvider.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,47 @@ import {
3030
GoogleFontsLoader,
3131
IEGlobalStyle,
3232
theme as defaultTheme,
33-
ThemeCustomizations,
3433
} from '@looker/design-tokens'
3534
import React, { FC, useMemo } from 'react'
3635
import { ScrollLockProvider } from './ScrollLock'
3736
import { ThemeProvider, ThemeProviderProps } from './ThemeProvider'
37+
import { ExtendComponentsTheme } from './ExtendComponentsProvider'
3838

39-
/**
40-
* Currently this is a simple pass-through to our ThemeProvider but it provides
41-
* the foundation of for hanging other context or setup code that might be needed
42-
* to stand-up @looker/components (a likely near-future candidate is i18next)
43-
*/
44-
45-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
46-
export interface ComponentsProviderProps extends ThemeProviderProps {
39+
export interface ComponentsProviderProps
40+
extends ThemeProviderProps,
41+
ExtendComponentsTheme {
4742
/**
4843
* Prevent automatic injection of a basic CSS-reset into the DOM
4944
* @default true
5045
*/
5146
globalStyle?: boolean
52-
5347
/**
5448
* Load fonts from the Google Fonts CDN if not already available
5549
* @default false
5650
*/
5751
loadGoogleFonts?: boolean
58-
5952
/**
6053
* Enable style support for IE11
6154
* @default false
6255
*/
6356
ie11Support?: boolean
64-
65-
themeCustomizations?: ThemeCustomizations
6657
}
6758

59+
/**
60+
* ComponentsProvider registers fundamental infrastructure for @looker/components to
61+
* initialize its components. Most components assume that they will be wrapped in a
62+
* <ComponentsProvider> to be able to successfully initialize.
63+
*
64+
* It provides a variety of features including
65+
* - ThemeProvider
66+
* - ScrollLockProvider
67+
*
68+
* Optionally, in also includes
69+
* - Internet Explorer 11 compatible styles
70+
* - Global style registration
71+
* - GoogleFont loading
72+
*
73+
*/
6874
export const ComponentsProvider: FC<ComponentsProviderProps> = ({
6975
children,
7076
globalStyle = true,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2020 Looker Data Sciences, Inc.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
import { generateTheme, ThemeCustomizations } from '@looker/design-tokens'
28+
import { ThemeContext, ThemeProvider } from 'styled-components'
29+
import React, { FC, useMemo, useContext } from 'react'
30+
31+
export interface ExtendComponentsTheme {
32+
themeCustomizations?: ThemeCustomizations
33+
}
34+
35+
/**
36+
* This component is designed for making adjustments to the Theme without
37+
* initializing an entire ComponentsProvider. ExtendComponentsThemeProvider
38+
* will merge the themeCustomizations with the theme already established.
39+
*
40+
* This component is intended for use cases where a portion of page carries
41+
* a different theme than the rest.
42+
*/
43+
export const ExtendComponentsThemeProvider: FC<ExtendComponentsTheme> = ({
44+
children,
45+
themeCustomizations,
46+
}) => {
47+
const parentTheme = useContext(ThemeContext)
48+
49+
const theme = useMemo(() => {
50+
return generateTheme(parentTheme, themeCustomizations)
51+
}, [parentTheme, themeCustomizations])
52+
53+
return <ThemeProvider theme={theme}>{children}</ThemeProvider>
54+
}

packages/components-providers/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
2525
*/
2626
export * from './ComponentsProvider'
27+
export * from './ExtendComponentsProvider'
2728
export * from './ScrollLock'

packages/components/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [UNRELEASED]
9+
10+
### Added
11+
12+
- `ExtendComponentsProvider`
13+
814
## [0.9.23]
915

1016
### Added

packages/components/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export * from './VisuallyHidden'
5959

6060
export * from './utils'
6161

62-
export { ComponentsProvider } from '@looker/components-providers'
62+
export {
63+
ComponentsProvider,
64+
ExtendComponentsThemeProvider,
65+
} from '@looker/components-providers'
6366

64-
/** Provided here for backwards compatibility.
65-
* @TODO - Remove before 1.0
66-
**/
6767
export { theme, Theme } from '@looker/design-tokens'

0 commit comments

Comments
 (0)