Skip to content

Commit

Permalink
feat(theming): add :focus-visible scoping <div> knock-out option to T…
Browse files Browse the repository at this point in the history
…hemeProvider (#897)
  • Loading branch information
jzempel committed Oct 16, 2020
1 parent a088c95 commit 28909c0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
14 changes: 7 additions & 7 deletions packages/theming/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"index.cjs.js": {
"bundled": 22430,
"minified": 14157,
"gzipped": 5188
"bundled": 22511,
"minified": 14210,
"gzipped": 5201
},
"index.esm.js": {
"bundled": 21591,
"minified": 13393,
"gzipped": 5068,
"bundled": 21652,
"minified": 13429,
"gzipped": 5080,
"treeshaked": {
"rollup": {
"code": 3408,
"import_statements": 146
},
"webpack": {
"code": 7327
"code": 7361
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions packages/theming/src/elements/ThemeProvider.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React from 'react';
import { render } from 'garden-test-utils';
import ThemeProvider from './ThemeProvider';

describe('ThemeProvider', () => {
it('renders a :focus-visible scoping <div> by default', () => {
const { container } = render(<ThemeProvider />);

expect(container.firstChild!.nodeName).toBe('DIV');
});

it('only renders children when focusVisibleRef is null', () => {
const { container } = render(
<ThemeProvider focusVisibleRef={null}>
<button />
</ThemeProvider>
);

expect(container.firstChild!.nodeName).toBe('BUTTON');
});
});
24 changes: 20 additions & 4 deletions packages/theming/src/elements/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ import DEFAULT_THEME from './theme';
import { useDocument } from '../utils/useDocument';

interface IGardenThemeProviderProps extends Partial<ThemeProps<DefaultTheme>> {
focusVisibleRef?: React.RefObject<HTMLElement>;
/**
* Provides values for component styling. See styled-components
* [`ThemeProvider`](https://styled-components.com/docs/api#themeprovider)
* for details.
*/
theme?: DefaultTheme;
/**
* Provides a reference to the DOM node used to scope a `:focus-visible`
* polyfill. If left `undefined`, a scoping `<div>` will be rendered.
* Assigning `null` (on a nested `ThemeProvider`, for example) prevents the
* added polyfill and scoping `<div>`.
*/
focusVisibleRef?: React.RefObject<HTMLElement> | null;
}

const GardenThemeProvider: React.FunctionComponent<IGardenThemeProviderProps> = ({
Expand All @@ -24,13 +36,18 @@ const GardenThemeProvider: React.FunctionComponent<IGardenThemeProviderProps> =
}) => {
const scopeRef = useRef<HTMLDivElement>(null);
const relativeDocument = useDocument(theme);
const controlledScopeRef = getControlledValue(focusVisibleRef, scopeRef);
const controlledScopeRef =
focusVisibleRef === null ? React.createRef() : getControlledValue(focusVisibleRef, scopeRef);

useFocusVisible({ scope: controlledScopeRef, relativeDocument });

return (
<ThemeProvider theme={theme!} {...other}>
{focusVisibleRef ? (children as any) : <div ref={scopeRef}>{children as any}</div>}
{focusVisibleRef === undefined ? (
<div ref={scopeRef}>{children as any}</div>
) : (
(children as any)
)}
</ThemeProvider>
);
};
Expand All @@ -39,5 +56,4 @@ GardenThemeProvider.defaultProps = {
theme: DEFAULT_THEME
};

/** @component */
export default GardenThemeProvider;
6 changes: 5 additions & 1 deletion packages/theming/styleguide.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {
content: '../../packages/theming/README.md'
},
{
name: 'Elements',
name: 'Examples',
sections: [
{
name: 'ThemeProvider',
Expand All @@ -44,6 +44,10 @@ module.exports = {
}
]
},
{
name: 'Elements',
components: '../../packages/theming/src/elements/[A-Z]*.{ts,tsx}'
},
{
name: 'Utils',
components: '../../packages/theming/src/utils/*.{ts,tsx}'
Expand Down

0 comments on commit 28909c0

Please sign in to comment.