-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(progress-tracker): add base markup for progress-tracker
- Loading branch information
Showing
8 changed files
with
160 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 46 additions & 4 deletions
50
packages/components/progress-tracker/src/ProgressTracker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,51 @@ | ||
import { ComponentPropsWithoutRef, forwardRef, PropsWithChildren } from 'react' | ||
import { type ComponentPropsWithoutRef, forwardRef, type PropsWithChildren, useState } from 'react' | ||
|
||
export type ProgressTrackerProps = ComponentPropsWithoutRef<'div'> | ||
import { ProgressTrackerContext } from './ProgressTrackerContext' | ||
|
||
export interface ProgressTrackerProps extends ComponentPropsWithoutRef<'div'> { | ||
/** | ||
* The index of the current step. | ||
* @default 0 | ||
*/ | ||
stepIndex?: number | ||
/** | ||
* Event handler called when clicking on a step. | ||
*/ | ||
onStepClick?: (stepId: string) => void | ||
/** | ||
* Sets the component as interactive or not. | ||
* @default false | ||
*/ | ||
readOnly?: boolean | ||
} | ||
|
||
export const ProgressTracker = forwardRef<HTMLDivElement, PropsWithChildren<ProgressTrackerProps>>( | ||
(props, ref) => { | ||
return <div ref={ref} {...props} /> | ||
( | ||
{ | ||
stepIndex: propStepIndex = 0, | ||
onStepClick: onStepClickProp, | ||
readOnly = false, | ||
children, | ||
...rest | ||
}, | ||
ref | ||
) => { | ||
const [steps, setSteps] = useState<Set<string>>(new Set()) | ||
const [stepIndex, setStepIndex] = useState<number>(propStepIndex) | ||
|
||
const onStepClick = (stepId: string) => { | ||
setStepIndex([...steps].indexOf(stepId)) | ||
onStepClickProp?.(stepId) | ||
} | ||
|
||
const Component = readOnly ? 'div' : 'nav' | ||
|
||
return ( | ||
<ProgressTrackerContext.Provider value={{ stepIndex, onStepClick, steps, setSteps }}> | ||
<Component ref={ref} {...rest}> | ||
<ol>{children}</ol> | ||
</Component> | ||
</ProgressTrackerContext.Provider> | ||
) | ||
} | ||
) |
16 changes: 16 additions & 0 deletions
16
packages/components/progress-tracker/src/ProgressTrackerContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createContext, type Dispatch, type SetStateAction, useContext } from 'react' | ||
|
||
import type { ProgressTrackerProps } from './ProgressTracker' | ||
|
||
export type ProgressTrackerContextInterface = Required< | ||
Pick<ProgressTrackerProps, 'stepIndex' | 'onStepClick'> | ||
> & { | ||
steps: Set<string> | ||
setSteps: Dispatch<SetStateAction<Set<string>>> | ||
} | ||
|
||
export const ProgressTrackerContext = createContext<ProgressTrackerContextInterface>( | ||
{} as ProgressTrackerContextInterface | ||
) | ||
|
||
export const useProgressTrackerContext = () => useContext(ProgressTrackerContext) |
20 changes: 20 additions & 0 deletions
20
packages/components/progress-tracker/src/ProgressTrackerStep.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { cva, type VariantProps } from 'class-variance-authority' | ||
|
||
export const progressTrackerStepVariants = cva(['inline-flex', 'bg-main'], { | ||
variants: { | ||
active: { | ||
true: 'bg-success', | ||
false: '', | ||
}, | ||
disabled: { | ||
true: 'bg-info', | ||
false: '', | ||
}, | ||
}, | ||
defaultVariants: { | ||
active: false, | ||
disabled: false, | ||
}, | ||
}) | ||
|
||
export type ProgressTrackerStepVariantsProps = VariantProps<typeof progressTrackerStepVariants> |
39 changes: 39 additions & 0 deletions
39
packages/components/progress-tracker/src/ProgressTrackerStep.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { type ComponentPropsWithoutRef, forwardRef, useEffect, useId, useState } from 'react' | ||
|
||
import { useProgressTrackerContext } from './ProgressTrackerContext' | ||
import { progressTrackerStepVariants } from './ProgressTrackerStep.styles' | ||
|
||
export type ProgressTrackerStepProps = ComponentPropsWithoutRef<'li'> & { | ||
label?: string | ||
} | ||
|
||
export const ProgressTrackerStep = forwardRef<HTMLLIElement, ProgressTrackerStepProps>( | ||
({ label, className, ...rest }, ref) => { | ||
const { stepIndex: activeStepIndex, steps, onStepClick, setSteps } = useProgressTrackerContext() | ||
|
||
const ID = useId() | ||
const stepId = `step-${ID}` | ||
|
||
const [isActive, setIsActive] = useState(false) | ||
|
||
useEffect(() => setSteps(steps => steps.add(stepId)), []) | ||
|
||
useEffect( | ||
() => setIsActive([...steps].indexOf(stepId) === activeStepIndex), | ||
[activeStepIndex, steps, stepId] | ||
) | ||
|
||
return ( | ||
<li | ||
id={stepId} | ||
ref={ref} | ||
className={progressTrackerStepVariants({ active: isActive, className })} | ||
{...rest} | ||
> | ||
<button type="button" onClick={() => onStepClick(stepId)}> | ||
<span>{label}</span> | ||
</button> | ||
</li> | ||
) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
export { ProgressTracker } from './ProgressTracker' | ||
import type { FC } from 'react' | ||
|
||
import { ProgressTracker as Root, type ProgressTrackerProps } from './ProgressTracker' | ||
import { ProgressTrackerStep as Step, type ProgressTrackerStepProps } from './ProgressTrackerStep' | ||
|
||
export const ProgressTracker: FC<ProgressTrackerProps> & { | ||
Step: typeof Step | ||
} = Object.assign(Root, { | ||
Step, | ||
}) | ||
|
||
ProgressTracker.displayName = 'ProgressTracker' | ||
Step.displayName = 'ProgressTracker.Step' | ||
|
||
export type { ProgressTrackerProps, ProgressTrackerStepProps } |