Skip to content

Commit

Permalink
feat(DataBar)!: remove top-level sub-component(s) (#1686)
Browse files Browse the repository at this point in the history
- in newly composed component also use code block for prop documentation
  • Loading branch information
booc0mtaco committed Jul 19, 2023
1 parent 8743e62 commit b4b9276
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 113 deletions.
37 changes: 37 additions & 0 deletions src/components/DataBar/DataBar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,40 @@
.data-bar__segment-space--incomplete {
width: calc(100% + 2 * var(--eds-theme-border-width) - 0.5 * var(--data-bar-height));
}

/*------------------------------------*\
# DATA BAR SEGMENT
\*------------------------------------*/

/**
* A block that represents its progress in a data bar.
*/
.data-bar-segment {
height: 100%;
}

/**
* The pointer cursor indicates that the element is hoverable.
* The non hoverable case is the default cursor, used for the empty state indicator.
*/
.data-bar-segment--hoverable:hover {
cursor: pointer;
}

/**
* The color variants of the data bar segments.
*/
.data-bar-segment--brand {
background-color: var(--eds-theme-color-background-brand-primary-strong);
}
/* prettier-ignore */
.data-bar-segment--brand.data-bar-segment--hoverable:hover {
background-color: var(--eds-theme-color-background-brand-primary-strong-hover);
}

.data-bar-segment--success {
background-color: var(--eds-theme-color-icon-utility-success);
}
.data-bar-segment--success.data-bar-segment--hoverable:hover {
background-color: var(--eds-theme-color-icon-utility-success-hover);
}
84 changes: 76 additions & 8 deletions src/components/DataBar/DataBar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import clsx from 'clsx';
import React from 'react';
import { useId } from '../../util/useId';

import DataBarSegment from '../DataBarSegment';
import type { Variants } from '../DataBarSegment';
import Text from '../Text';
import Tooltip from '../Tooltip';

import styles from './DataBar.module.css';

type Segment = {
Expand All @@ -18,7 +17,28 @@ type Segment = {
value: number;
};

export type Props = {
type Variant = 'brand' | 'success';

export type DataBarSegmentProps = {
/**
* CSS class names that can be appended to the component.
*/
className?: string;
/**
* Tooltip text to be displayed when the segment is hovered.
*/
text?: string;
/**
* Width that the segment should consume.
*/
width: string;
/**
* Color variant of the individual segment.
*/
variant?: Variant;
} & React.HTMLAttributes<HTMLElement>;

export type DataBarProps = {
/**
* CSS class names that can be appended to the component.
*/
Expand All @@ -39,7 +59,7 @@ export type Props = {
/**
* Color variant of the data bar. Decorates the segments.
*/
variant?: Variants;
variant?: Variant;
} & React.HTMLAttributes<HTMLElement>;

/**
Expand Down Expand Up @@ -67,7 +87,7 @@ export const DataBar = ({
segments,
variant = 'brand',
...other
}: Props) => {
}: DataBarProps) => {
/**
* Calculates the total of the segment values.
*/
Expand Down Expand Up @@ -119,7 +139,7 @@ export const DataBar = ({
/* Ensures a minimumum width of 5% for the segment. */
const percentage = Math.max(5, (segment.value / max) * 100);
segmentComponents.push(
<DataBarSegment
<DataBar.Segment
aria-label={segment.text}
key={`segment-${index}`}
onKeyDown={(e) => handleOnKeyDown(e, index)}
Expand All @@ -138,7 +158,7 @@ export const DataBar = ({
*/
if (!segmentComponents.length) {
segmentComponents.push(
<DataBarSegment
<DataBar.Segment
key="segment-empty"
role="gridcell"
variant={variant}
Expand Down Expand Up @@ -184,3 +204,51 @@ export const DataBar = ({
</div>
);
};

/**
* A segment sub component for the <DataBar>.
*
* Example usage:
*
* ```tsx
* <DataBar.Segment text="Segment 1" width="40%" />
* ```
*/
const DataBarSegment = React.forwardRef(
(
{
className,
text,
width,
variant = 'brand',
...other
}: DataBarSegmentProps,
ref: React.ForwardedRef<HTMLDivElement>,
) => {
const componentClassName = clsx(
styles['data-bar-segment'],
styles[`data-bar-segment--${variant}`],
text && styles['data-bar-segment--hoverable'],
className,
);
const segmentComponent = (
<div
className={componentClassName}
ref={ref}
style={{ width: `${width}` }}
{...other}
/>
);
return text ? (
<Tooltip align="bottom" text={text}>
{segmentComponent}
</Tooltip>
) : (
segmentComponent
);
},
);

DataBarSegment.displayName = 'DataBarSegment'; // Satisfy eslint

DataBar.Segment = DataBarSegment;
35 changes: 0 additions & 35 deletions src/components/DataBarSegment/DataBarSegment.module.css

This file was deleted.

67 changes: 0 additions & 67 deletions src/components/DataBarSegment/DataBarSegment.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/DataBarSegment/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export { default as CheckboxInput } from './components/CheckboxInput';
export { default as CheckboxLabel } from './components/CheckboxLabel';
export { default as ClickableStyle } from './components/ClickableStyle';
export { default as DataBar } from './components/DataBar';
export { default as DataBarSegment } from './components/DataBarSegment';
export { default as DragDrop } from './components/DragDrop';
export { default as DragDropContainer } from './components/DragDropContainer';
export { default as DragDropContainerHeader } from './components/DragDropContainerHeader';
Expand Down

0 comments on commit b4b9276

Please sign in to comment.