Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(UIShell): add prefix context refactor #9690

18 changes: 18 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7639,6 +7639,24 @@ Map {
},
},
"HeaderNavigation" => Object {
"contextType": Object {
"$$typeof": Symbol(react.context),
"Consumer": Object {
"$$typeof": Symbol(react.context),
"_calculateChangedBits": null,
"_context": [Circular],
},
"Provider": Object {
"$$typeof": Symbol(react.provider),
"_context": [Circular],
},
"_calculateChangedBits": null,
"_currentRenderer": null,
"_currentRenderer2": null,
"_currentValue": "bx",
"_currentValue2": "bx",
"_threadCount": 0,
},
"propTypes": Object {
"aria-label": [Function],
"aria-labelledby": [Function],
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/Content.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

const Content = ({
className: customClassName,
children,
tagName,
...rest
}) => {
const prefix = usePrefix();
const className = cx(`${prefix}--content`, customClassName);
return React.createElement(
tagName,
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

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

return (
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/HeaderGlobalAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
import Button from '../Button';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

/**
* HeaderGlobalAction is used as a part of the `HeaderGlobalBar`. It is
Expand All @@ -35,6 +33,7 @@ const HeaderGlobalAction = React.forwardRef(function HeaderGlobalAction(
},
ref
) {
const prefix = usePrefix();
const className = cx({
[customClassName]: !!customClassName,
[`${prefix}--header__action`]: true,
Expand Down
20 changes: 9 additions & 11 deletions packages/react/src/components/UIShell/HeaderMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
*/

import { ChevronDown16 } from '@carbon/icons-react';
import { settings } from 'carbon-components';
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import { keys, matches } from '../../internal/keyboard';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';

const { prefix } = settings;

const defaultRenderMenuContent = () => (
<ChevronDown16 className={`${prefix}--header__menu-arrow`} />
);
import { PrefixContext } from '../../internal/usePrefix';

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

static defaultProps = {
renderMenuContent: defaultRenderMenuContent,
};
static contextType = PrefixContext;

_subMenus = React.createRef();

Expand Down Expand Up @@ -166,6 +158,7 @@ class HeaderMenu extends React.Component {
};

render() {
const prefix = this.context;
const {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
Expand All @@ -176,6 +169,7 @@ class HeaderMenu extends React.Component {
focusRef, // eslint-disable-line no-unused-vars
...rest
} = this.props;

const accessibilityLabel = {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
Expand Down Expand Up @@ -205,7 +199,11 @@ class HeaderMenu extends React.Component {
tabIndex={0}
{...accessibilityLabel}>
{menuLinkName}
<MenuContent />
{MenuContent ? (
<MenuContent />
) : (
<ChevronDown16 className={`${this.context}--header__menu-arrow`} />
)}
</a>
<ul
{...accessibilityLabel}
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/HeaderMenuButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

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

import { settings } from 'carbon-components';
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

const HeaderMenuButton = ({
'aria-label': ariaLabel,
Expand All @@ -26,6 +24,7 @@ const HeaderMenuButton = ({
isCollapsible,
...rest
}) => {
const prefix = usePrefix();
const className = cx({
[customClassName]: !!customClassName,
[`${prefix}--header__action`]: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/HeaderMenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import PropTypes from 'prop-types';
import React from 'react';
import cx from 'classnames';
import Link, { LinkPropTypes } from './Link';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

const HeaderMenuItem = React.forwardRef(function HeaderMenuItem(
{
Expand All @@ -24,6 +22,7 @@ const HeaderMenuItem = React.forwardRef(function HeaderMenuItem(
},
ref
) {
const prefix = usePrefix();
const linkClassName = cx({
[`${prefix}--header__menu-item`]: true,
// We set the current class only if `isCurrentPage` is passed in and we do
Expand Down
6 changes: 2 additions & 4 deletions packages/react/src/components/UIShell/HeaderName.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import Link, { LinkPropTypes } from './Link';

const { prefix: selectorPrefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

const HeaderName = ({
children,
Expand All @@ -20,6 +17,7 @@ const HeaderName = ({
href,
...rest
}) => {
const selectorPrefix = usePrefix();
const className = cx(`${selectorPrefix}--header__name`, customClassName);
return (
<Link {...rest} className={className} href={href}>
Expand Down
7 changes: 4 additions & 3 deletions packages/react/src/components/UIShell/HeaderNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';

const { prefix } = settings;
import { PrefixContext } from '../../internal/usePrefix';

export default class HeaderNavigation extends React.Component {
static propTypes = {
Expand Down Expand Up @@ -40,6 +38,8 @@ export default class HeaderNavigation extends React.Component {
};
}

static contextType = PrefixContext;

/**
* Handles individual menuitem refs. We assign them to a class instance
* property so that we can properly manage focus of our children.
Expand All @@ -49,6 +49,7 @@ export default class HeaderNavigation extends React.Component {
};

render() {
const prefix = this.context;
const {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/HeaderPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
*/

import React from 'react';
import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

const HeaderPanel = React.forwardRef(function HeaderPanel(
{
Expand All @@ -24,6 +22,7 @@ const HeaderPanel = React.forwardRef(function HeaderPanel(
},
ref
) {
const prefix = usePrefix();
const accessibilityLabel = {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/HeaderSideNavItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

const HeaderSideNavItems = ({
className: customClassName,
children,
hasDivider,
}) => {
const prefix = usePrefix();
const className = cx(
{
[`${prefix}--side-nav__header-navigation`]: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/SideNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
*/

import React, { useState, useRef } from 'react';
import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
import { CARBON_SIDENAV_ITEMS } from './_utils';
import { usePrefix } from '../../internal/usePrefix';
// TO-DO: comment back in when footer is added for rails
// import SideNavFooter from './SideNavFooter';

const { prefix } = settings;

const SideNav = React.forwardRef(function SideNav(props, ref) {
const {
expanded: expandedProp,
Expand All @@ -37,6 +35,7 @@ const SideNav = React.forwardRef(function SideNav(props, ref) {
...other
} = props;

const prefix = usePrefix();
const { current: controlled } = useRef(expandedProp !== undefined);
const [expandedState, setExpandedState] = useState(defaultExpanded);
const [expandedViaHoverState, setExpandedViaHoverState] = useState(
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/SideNavDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

const SideNavDetails = ({ children, className: customClassName, title }) => {
const prefix = usePrefix();
const className = cx(`${prefix}--side-nav__details`, customClassName);
return (
<div className={className}>
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/SideNavDivider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

function SideNavDivider({ className }) {
const prefix = usePrefix();
const classNames = cx(`${prefix}--side-nav__divider`, className);
return <li role="separator" className={classNames} />;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/SideNavFooter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

import { Close20, ChevronRight20 } from '@carbon/icons-react';

import { settings } from 'carbon-components';
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

/**
* SideNavFooter is used for rendering the button at the bottom of the side
Expand All @@ -25,6 +23,7 @@ const SideNavFooter = ({
expanded,
onToggle,
}) => {
const prefix = usePrefix();
const className = cx(`${prefix}--side-nav__footer`, customClassName);
return (
<footer className={className}>
Expand Down
5 changes: 2 additions & 3 deletions packages/react/src/components/UIShell/SideNavHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

import { settings } from 'carbon-components';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import SideNavIcon from './SideNavIcon';

const { prefix } = settings;
import { usePrefix } from '../../internal/usePrefix';

const SideNavHeader = ({
className: customClassName,
children,
renderIcon: IconElement,
}) => {
const prefix = usePrefix();
const className = cx(`${prefix}--side-nav__header`, customClassName);
return (
<header className={className}>
Expand Down
Loading