Skip to content

Commit

Permalink
Add progress bar to ds (#1787)
Browse files Browse the repository at this point in the history
* feat: add progress bar to ds

* chore: update progress bar according to review
  • Loading branch information
kalashshah authored and corlard3y committed Aug 1, 2024
1 parent 8d2d695 commit dbd05fd
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { Link, type LinkProps } from './link';
export { Lozenge, type LozengeProps } from './lozenge';
export { Menu, type MenuProps, MenuItem, type MenuItemComponentProps } from './menu';
export { Modal, type ModalProps, modal } from './modal';
export { ProgressBar, type ProgressBarProps } from './progressBar';
export { Separator, type SeparatorProps } from './separator';
export { Skeleton, type SkeletonProps } from './skeleton';
export { Tabs, type TabsProps, type TabItem } from './tabs';
Expand Down
41 changes: 41 additions & 0 deletions src/blocks/progressBar/ProgressBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { FC } from 'react';
import styled, { FlattenSimpleInterpolation } from 'styled-components';

import type { ProgressBarProps } from './ProgressBar.types';
import { getProgressWidthPercentage } from './ProgressBar.utils';

const StyledProgressBarContainer = styled.div<{ css?: FlattenSimpleInterpolation }>`
/* Default CSS */
background-color: var(--components-progress-bar-background-default);
width: 100%;
height: 4px;
border-radius: var(--radius-xxs, 8px);
/* Extra CSS prop */
${({ css }) => css || ''}
`;

const StyledProgressBar = styled.div<{ width: string }>`
/* Default CSS */
border-radius: var(--radius-xxs, 8px);
background-color: var(--components-progress-bar-background-progress);
height: 100%;
width: ${({ width }) => width};
transition: width 0.3s ease;
`;

const ProgressBar: FC<ProgressBarProps> = ({ progress, max = 100, ...rest }) => (
<StyledProgressBarContainer
role="progressbar"
aria-valuemin={0}
aria-valuemax={max}
aria-valuenow={progress}
{...rest}
>
<StyledProgressBar width={`${getProgressWidthPercentage(progress, max)}%`} />
</StyledProgressBarContainer>
);

ProgressBar.displayName = 'ProgressBar';

export { ProgressBar };
11 changes: 11 additions & 0 deletions src/blocks/progressBar/ProgressBar.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TransformedHTMLAttributes } from 'blocks/Blocks.types';
import { FlattenSimpleInterpolation } from 'styled-components';

export type ProgressBarProps = {
/* Additional prop from styled components to apply custom css to ProgressBar */
css?: FlattenSimpleInterpolation;
/* Progress value */
progress: number;
/* Max value */
max?: number;
} & TransformedHTMLAttributes<HTMLDivElement>;
4 changes: 4 additions & 0 deletions src/blocks/progressBar/ProgressBar.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getProgressWidthPercentage = (progress: number, total = 100) => {
const percentage = (progress / total) * 100;
return Math.min(Math.max(percentage, 0), 100);
};
3 changes: 3 additions & 0 deletions src/blocks/progressBar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './ProgressBar';
export * from './ProgressBar.utils';
export * from './ProgressBar.types';
4 changes: 4 additions & 0 deletions src/blocks/theme/colors/colors.semantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { dropdownSemantics } from '../semantics/semantics.dropdown';
import { iconSemantics } from '../semantics/semantics.icon';
import { inputSemantics } from '../semantics/semantics.input';
import { modalSemantics } from '../semantics/semantics.modal';
import { progressBarSemantics } from '../semantics/semantics.progress-bar';
import { radioSemantics } from '../semantics/semantics.radio';
import { skeletonSemantics } from '../semantics/semantics.skeleton';
import { strokeSemantics } from '../semantics/semantics.stroke';
Expand All @@ -37,6 +38,7 @@ type SemanticKeys = {
icon: 'icon';
input: 'components-inputs';
modal: 'components-modal';
progressBar: 'components-progress-bar';
radio: 'components-radio-button';
surface: 'surface';
stroke: 'stroke';
Expand All @@ -62,6 +64,7 @@ export const semanticKeys: SemanticKeys = {
icon: 'icon',
input: 'components-inputs',
modal: 'components-modal',
progressBar: 'components-progress-bar',
radio: 'components-radio-button',
surface: 'surface',
stroke: 'stroke',
Expand All @@ -87,6 +90,7 @@ export const colorSemantics = {
[semanticKeys.icon]: iconSemantics,
[semanticKeys.input]: inputSemantics,
[semanticKeys.modal]: modalSemantics,
[semanticKeys.progressBar]: progressBarSemantics,
[semanticKeys.radio]: radioSemantics,
[semanticKeys.surface]: surfaceSemantics,
[semanticKeys.stroke]: strokeSemantics,
Expand Down
6 changes: 6 additions & 0 deletions src/blocks/theme/semantics/semantics.progress-bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { surfaceSemantics } from './semantics.surface';

export const progressBarSemantics = {
'background-default': { light: surfaceSemantics['secondary'].light, dark: surfaceSemantics['secondary'].dark },
'background-progress': { light: surfaceSemantics['brand-medium'].light, dark: surfaceSemantics['brand-medium'].dark },
};

0 comments on commit dbd05fd

Please sign in to comment.