Skip to content

Commit 6b96dc0

Browse files
authored
feat(UIShell): add prefix context refactor (#9690)
* feat(UIShell): add prefix context refactor * chore(test): fix broken tests * chore(UIShell): remove unneeded prefixs * fix(UIShell): check for menu content and render chevron if undefined
1 parent e080720 commit 6b96dc0

32 files changed

+103
-117
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7639,6 +7639,24 @@ Map {
76397639
},
76407640
},
76417641
"HeaderNavigation" => Object {
7642+
"contextType": Object {
7643+
"$$typeof": Symbol(react.context),
7644+
"Consumer": Object {
7645+
"$$typeof": Symbol(react.context),
7646+
"_calculateChangedBits": null,
7647+
"_context": [Circular],
7648+
},
7649+
"Provider": Object {
7650+
"$$typeof": Symbol(react.provider),
7651+
"_context": [Circular],
7652+
},
7653+
"_calculateChangedBits": null,
7654+
"_currentRenderer": null,
7655+
"_currentRenderer2": null,
7656+
"_currentValue": "bx",
7657+
"_currentValue2": "bx",
7658+
"_threadCount": 0,
7659+
},
76427660
"propTypes": Object {
76437661
"aria-label": [Function],
76447662
"aria-labelledby": [Function],

packages/react/src/components/UIShell/Content.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { settings } from 'carbon-components';
98
import cx from 'classnames';
109
import PropTypes from 'prop-types';
1110
import React from 'react';
12-
13-
const { prefix } = settings;
11+
import { usePrefix } from '../../internal/usePrefix';
1412

1513
const Content = ({
1614
className: customClassName,
1715
children,
1816
tagName,
1917
...rest
2018
}) => {
19+
const prefix = usePrefix();
2120
const className = cx(`${prefix}--content`, customClassName);
2221
return React.createElement(
2322
tagName,

packages/react/src/components/UIShell/Header.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { settings } from 'carbon-components';
98
import cx from 'classnames';
109
import PropTypes from 'prop-types';
1110
import React from 'react';
1211
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
13-
14-
const { prefix } = settings;
12+
import { usePrefix } from '../../internal/usePrefix';
1513

1614
const Header = ({ className: customClassName, children, ...rest }) => {
15+
const prefix = usePrefix();
1716
const className = cx(`${prefix}--header`, customClassName);
1817

1918
return (

packages/react/src/components/UIShell/HeaderGlobalAction.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { settings } from 'carbon-components';
98
import cx from 'classnames';
109
import React from 'react';
1110
import PropTypes from 'prop-types';
1211
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
1312
import Button from '../Button';
14-
15-
const { prefix } = settings;
13+
import { usePrefix } from '../../internal/usePrefix';
1614

1715
/**
1816
* HeaderGlobalAction is used as a part of the `HeaderGlobalBar`. It is
@@ -35,6 +33,7 @@ const HeaderGlobalAction = React.forwardRef(function HeaderGlobalAction(
3533
},
3634
ref
3735
) {
36+
const prefix = usePrefix();
3837
const className = cx({
3938
[customClassName]: !!customClassName,
4039
[`${prefix}--header__action`]: true,

packages/react/src/components/UIShell/HeaderMenu.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66
*/
77

88
import { ChevronDown16 } from '@carbon/icons-react';
9-
import { settings } from 'carbon-components';
109
import cx from 'classnames';
1110
import React from 'react';
1211
import PropTypes from 'prop-types';
1312
import { keys, matches } from '../../internal/keyboard';
1413
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
15-
16-
const { prefix } = settings;
17-
18-
const defaultRenderMenuContent = () => (
19-
<ChevronDown16 className={`${prefix}--header__menu-arrow`} />
20-
);
14+
import { PrefixContext } from '../../internal/usePrefix';
2115

2216
/**
2317
* `HeaderMenu` is used to render submenu's in the `Header`. Most often children
@@ -53,9 +47,7 @@ class HeaderMenu extends React.Component {
5347
tabIndex: PropTypes.number,
5448
};
5549

56-
static defaultProps = {
57-
renderMenuContent: defaultRenderMenuContent,
58-
};
50+
static contextType = PrefixContext;
5951

6052
_subMenus = React.createRef();
6153

@@ -166,6 +158,7 @@ class HeaderMenu extends React.Component {
166158
};
167159

168160
render() {
161+
const prefix = this.context;
169162
const {
170163
'aria-label': ariaLabel,
171164
'aria-labelledby': ariaLabelledBy,
@@ -176,6 +169,7 @@ class HeaderMenu extends React.Component {
176169
focusRef, // eslint-disable-line no-unused-vars
177170
...rest
178171
} = this.props;
172+
179173
const accessibilityLabel = {
180174
'aria-label': ariaLabel,
181175
'aria-labelledby': ariaLabelledBy,
@@ -205,7 +199,11 @@ class HeaderMenu extends React.Component {
205199
tabIndex={0}
206200
{...accessibilityLabel}>
207201
{menuLinkName}
208-
<MenuContent />
202+
{MenuContent ? (
203+
<MenuContent />
204+
) : (
205+
<ChevronDown16 className={`${this.context}--header__menu-arrow`} />
206+
)}
209207
</a>
210208
<ul
211209
{...accessibilityLabel}

packages/react/src/components/UIShell/HeaderMenuButton.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
import { Close20, Menu20 } from '@carbon/icons-react';
99

10-
import { settings } from 'carbon-components';
1110
import cx from 'classnames';
1211
import React from 'react';
1312
import PropTypes from 'prop-types';
1413
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
15-
16-
const { prefix } = settings;
14+
import { usePrefix } from '../../internal/usePrefix';
1715

1816
const HeaderMenuButton = ({
1917
'aria-label': ariaLabel,
@@ -26,6 +24,7 @@ const HeaderMenuButton = ({
2624
isCollapsible,
2725
...rest
2826
}) => {
27+
const prefix = usePrefix();
2928
const className = cx({
3029
[customClassName]: !!customClassName,
3130
[`${prefix}--header__action`]: true,

packages/react/src/components/UIShell/HeaderMenuItem.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { settings } from 'carbon-components';
98
import PropTypes from 'prop-types';
109
import React from 'react';
1110
import cx from 'classnames';
1211
import Link, { LinkPropTypes } from './Link';
13-
14-
const { prefix } = settings;
12+
import { usePrefix } from '../../internal/usePrefix';
1513

1614
const HeaderMenuItem = React.forwardRef(function HeaderMenuItem(
1715
{
@@ -24,6 +22,7 @@ const HeaderMenuItem = React.forwardRef(function HeaderMenuItem(
2422
},
2523
ref
2624
) {
25+
const prefix = usePrefix();
2726
const linkClassName = cx({
2827
[`${prefix}--header__menu-item`]: true,
2928
// We set the current class only if `isCurrentPage` is passed in and we do

packages/react/src/components/UIShell/HeaderName.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
8-
import { settings } from 'carbon-components';
97
import cx from 'classnames';
108
import React from 'react';
119
import PropTypes from 'prop-types';
1210
import Link, { LinkPropTypes } from './Link';
13-
14-
const { prefix: selectorPrefix } = settings;
11+
import { usePrefix } from '../../internal/usePrefix';
1512

1613
const HeaderName = ({
1714
children,
@@ -20,6 +17,7 @@ const HeaderName = ({
2017
href,
2118
...rest
2219
}) => {
20+
const selectorPrefix = usePrefix();
2321
const className = cx(`${selectorPrefix}--header__name`, customClassName);
2422
return (
2523
<Link {...rest} className={className} href={href}>

packages/react/src/components/UIShell/HeaderNavigation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { settings } from 'carbon-components';
98
import cx from 'classnames';
109
import React from 'react';
1110
import PropTypes from 'prop-types';
1211
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
13-
14-
const { prefix } = settings;
12+
import { PrefixContext } from '../../internal/usePrefix';
1513

1614
export default class HeaderNavigation extends React.Component {
1715
static propTypes = {
@@ -40,6 +38,8 @@ export default class HeaderNavigation extends React.Component {
4038
};
4139
}
4240

41+
static contextType = PrefixContext;
42+
4343
/**
4444
* Handles individual menuitem refs. We assign them to a class instance
4545
* property so that we can properly manage focus of our children.
@@ -49,6 +49,7 @@ export default class HeaderNavigation extends React.Component {
4949
};
5050

5151
render() {
52+
const prefix = this.context;
5253
const {
5354
'aria-label': ariaLabel,
5455
'aria-labelledby': ariaLabelledBy,

packages/react/src/components/UIShell/HeaderPanel.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
*/
77

88
import React from 'react';
9-
import { settings } from 'carbon-components';
109
import cx from 'classnames';
1110
import PropTypes from 'prop-types';
1211
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
13-
14-
const { prefix } = settings;
12+
import { usePrefix } from '../../internal/usePrefix';
1513

1614
const HeaderPanel = React.forwardRef(function HeaderPanel(
1715
{
@@ -24,6 +22,7 @@ const HeaderPanel = React.forwardRef(function HeaderPanel(
2422
},
2523
ref
2624
) {
25+
const prefix = usePrefix();
2726
const accessibilityLabel = {
2827
'aria-label': ariaLabel,
2928
'aria-labelledby': ariaLabelledBy,

0 commit comments

Comments
 (0)