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

fix(TimelineStep): move subLabel below timeline #4202

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/orbit-components/src/Timeline/TimelineContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Context {
interface StepContext {
index: number;
last: boolean;
hasSubLabelMargin?: boolean;
}

export const TimelineStatusContext = React.createContext<Context>({
Expand All @@ -24,6 +25,7 @@ export const TimelineStatusContext = React.createContext<Context>({
export const TimelineStepContext = React.createContext<StepContext>({
index: 0,
last: false,
hasSubLabelMargin: false,
});

export const TimelineStatusProvider = ({ children, direction }) => {
Expand All @@ -37,8 +39,11 @@ export const TimelineStatusProvider = ({ children, direction }) => {
return <TimelineStatusContext.Provider value={value}>{children}</TimelineStatusContext.Provider>;
};

export const TimelineStepProvider = ({ children, index, last }) => {
const value = React.useMemo(() => ({ index, last }), [index, last]);
export const TimelineStepProvider = ({ children, index, last, hasSubLabelMargin }) => {
const value = React.useMemo(
() => ({ index, last, hasSubLabelMargin }),
[index, last, hasSubLabelMargin],
);
return <TimelineStepContext.Provider value={value}>{children}</TimelineStepContext.Provider>;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Text from "../../../Text";
import Stack from "../../../Stack";
import defaultTheme from "../../../defaultTheme";
import type { Props as StepProps, Type } from "../types";
import useTheme from "../../../hooks/useTheme";

const StyledDescription = styled.div<{ active?: boolean }>`
${({ theme, active }) => css`
Expand All @@ -28,6 +29,7 @@ interface Props extends StepProps {
last: boolean;
prevType: Type;
nextType: Type;
hasSubLabelMargin?: boolean;
}

const TimelineStepDesktop = ({
Expand All @@ -39,14 +41,12 @@ const TimelineStepDesktop = ({
active,
label,
subLabel,
hasSubLabelMargin,
}: Props) => {
const theme = useTheme();

return (
<Stack inline shrink direction="column" align="center">
<StyledText>
<Text align="center" size="small">
{subLabel}
</Text>
</StyledText>
<StyledRelative inner>
<StyledProgressLine desktop status={type} prevStatus={prevType} nextStatus={nextType} />
<TypeIcon type={type} active={!!active} />
Expand All @@ -59,8 +59,19 @@ const TimelineStepDesktop = ({
/>
</StyledRelative>
<Stack flex align="center" spacing="XSmall" direction="column">
{subLabel && (
<StyledText>
<Text align="center" size="small">
{subLabel}
</Text>
</StyledText>
)}
<StyledText active={active || (last && type === "success")}>
<Text align="center" weight="bold">
<Text
align="center"
weight="bold"
margin={{ top: !subLabel && hasSubLabelMargin ? theme.orbit.spaceLarge : 0 }}
>
{label}
</Text>
</StyledText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const getActiveState = ({

const TimelineStep = ({ children, label, subLabel, type, active }: Props) => {
const { types, setTypes, isColumnOnDesktop } = useStatuses();
const { index, last } = useStep();
const { index, last, hasSubLabelMargin } = useStep();
const { isDesktop } = useMediaQuery();

const nextType = types[index + 1];
Expand Down Expand Up @@ -57,6 +57,7 @@ const TimelineStep = ({ children, label, subLabel, type, active }: Props) => {
<TimelineStepDesktop
nextType={nextType}
prevType={prevType}
hasSubLabelMargin={hasSubLabelMargin}
label={label}
last={last}
active={isActive}
Expand Down
11 changes: 10 additions & 1 deletion packages/orbit-components/src/Timeline/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ const Timeline = ({ children, spaceAfter, direction, dataTest, id }: Props) => {
return isDesktop ? "row" : "column";
};

const hasSubLabelMargin = React.useMemo(
() => childrenArr.some(child => React.isValidElement(child) && child.props.subLabel),
[childrenArr],
);

return childrenArr && childrenArr.length > 0 ? (
<WrapperStyled spaceAfter={spaceAfter} data-test={dataTest} id={id}>
<Stack flex shrink direction={getDirection()}>
<TimelineStatusProvider direction={direction}>
{React.Children.map(childrenArr, (child, i) => {
if (React.isValidElement(child)) {
return (
<TimelineStepProvider index={i} last={i + 1 === childrenArr.length}>
<TimelineStepProvider
index={i}
last={i + 1 === childrenArr.length}
hasSubLabelMargin={hasSubLabelMargin}
>
{child}
</TimelineStepProvider>
);
Expand Down
Loading