Skip to content

Commit

Permalink
Add support for beta/new badges to subitems in nav and subheadings
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Jul 7, 2023
1 parent a09a0d7 commit de3cfc2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
9 changes: 3 additions & 6 deletions src-docs/src/components/guide_page/_guide_page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@
}

.guideSideNav__itemBadge {
margin-inline: $euiSizeXS;
}

// Shift the margin on the badge when selected and the dropdown arrow no longer shows
.euiSideNavItemButton-isSelected .guideSideNav__itemBadge {
margin-right: 0;
margin-inline-start: $euiSizeXS;
// Decrease distance from right side to allow for longer titles and sub-items
margin-inline-end: -$euiSizeS;
}
}

Expand Down
33 changes: 21 additions & 12 deletions src-docs/src/components/guide_page/guide_page_chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ export class GuidePageChrome extends Component {
});
};

renderSideNavBadge = ({ isBeta, isNew }) => {
if (isBeta) {
return (
<EuiBadge color="warning" className="guideSideNav__itemBadge">
BETA
</EuiBadge>
);
}
if (isNew) {
return (
<EuiBadge color="accent" className="guideSideNav__itemBadge">
NEW
</EuiBadge>
);
}
return undefined;
};

scrollNavSectionIntoView = () => {
// wait a bit for react to blow away and re-create the DOM
// then scroll the selected nav section into view
Expand Down Expand Up @@ -80,7 +98,7 @@ export class GuidePageChrome extends Component {
return;
}

return subSectionsWithTitles.map(({ title, sections }) => {
return subSectionsWithTitles.map(({ title, isBeta, isNew, sections }) => {
const id = slugify(title);

const subSectionHref = `${href}/${id}`;
Expand Down Expand Up @@ -115,6 +133,7 @@ export class GuidePageChrome extends Component {
: '',
items: subItems,
forceOpen: !!searchTerm || isCurrentlyOpenSubSection,
icon: this.renderSideNavBadge({ isBeta, isNew }),
};
});
};
Expand Down Expand Up @@ -146,16 +165,6 @@ export class GuidePageChrome extends Component {

const href = `#/${path}`;

const badge = isBeta ? (
<EuiBadge color="warning" className="guideSideNav__itemBadge">
BETA
</EuiBadge>
) : isNew ? (
<EuiBadge color="accent" className="guideSideNav__itemBadge">
NEW
</EuiBadge>
) : undefined;

let visibleName = name;
if (searchTerm) {
visibleName = (
Expand All @@ -176,7 +185,7 @@ export class GuidePageChrome extends Component {
isSelected: item.path === this.props.currentRoute.path,
forceOpen: !!(searchTerm && hasMatchingSubItem),
className: 'guideSideNav__item',
icon: badge,
icon: this.renderSideNavBadge({ isBeta, isNew }),
};
});

Expand Down
12 changes: 11 additions & 1 deletion src-docs/src/components/guide_section/guide_section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface GuideSectionProps
> {
id?: string;
title?: string;
isBeta?: boolean;
isNew?: boolean;
text?: ReactNode;
source?: any[];
demo?: ReactNode;
Expand Down Expand Up @@ -83,6 +85,8 @@ export const GuideSectionCodeTypesMap = {
export const GuideSection: FunctionComponent<GuideSectionProps> = ({
id,
title,
isBeta,
isNew,
text,
demo,
fullScreen,
Expand Down Expand Up @@ -210,7 +214,13 @@ export const GuideSection: FunctionComponent<GuideSectionProps> = ({
className={classNames('guideSection', className)}
>
<EuiSpacer size={(color || title) && isLargeBreakpoint ? 'xxl' : 'xs'} />
<GuideSectionExampleText title={title} id={id} wrapText={wrapText}>
<GuideSectionExampleText
title={title}
id={id}
isBeta={isBeta}
isNew={isNew}
wrapText={wrapText}
>
{text}
</GuideSectionExampleText>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import React, { FunctionComponent, ReactNode } from 'react';
import { EuiSpacer } from '../../../../../src/components/spacer';
import { EuiTitle } from '../../../../../src/components/title';
import { EuiText } from '../../../../../src/components/text';

import {
EuiSpacer,
EuiTitle,
EuiText,
EuiBetaBadge,
} from '../../../../../src/components';

export const LANGUAGES = ['javascript', 'html'] as const;

type GuideSectionExampleText = {
title?: ReactNode;
id?: string;
isBeta?: boolean;
isNew?: boolean;
children?: ReactNode;
wrapText?: boolean;
};

export const GuideSectionExampleText: FunctionComponent<
GuideSectionExampleText
> = ({ title, id, children, wrapText = true }) => {
> = ({ title, id, isBeta, isNew, children, wrapText = true }) => {
let titleNode;

if (title) {
const badge = (isBeta || isNew) && (
<EuiBetaBadge label={isBeta ? 'Beta' : 'New'} color="accent" size="s" />
);

titleNode = (
<>
<EuiTitle>
<h2 id={id}>{title}</h2>
<h2 id={id}>
{title}
{badge && <>&emsp;{badge}</>}
</h2>
</EuiTitle>
<EuiSpacer size="m" />
</>
Expand Down
1 change: 1 addition & 0 deletions src-docs/src/views/provider/provider_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const ProviderExample = {
},
{
title: 'Component defaults',
isBeta: true,
text: (
<EuiText>
<EuiCallOut title="Beta status" iconType="beta">
Expand Down

0 comments on commit de3cfc2

Please sign in to comment.