-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
169 lines (156 loc) · 6 KB
/
index.jsx
File metadata and controls
169 lines (156 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {Box, useTheme, Text} from '@chakra-ui/react'
import PropTypes from 'prop-types'
import {useIntl} from 'react-intl'
const steps = ['Ordered', 'Dispatched', 'Out for delivery', 'Delivered']
const OrderStatusBar = ({currentStepLabel}) => {
const theme = useTheme()
const intl = useIntl()
const getLocalizedMessage = (status) => {
switch (status) {
case 'Ordered':
return intl.formatMessage({id: 'status_bar.ordered', defaultMessage: 'Ordered'})
case 'Dispatched':
return intl.formatMessage({
id: 'status_bar.dispatched',
defaultMessage: 'Dispatched'
})
case 'Out for delivery':
return intl.formatMessage({
id: 'status_bar.out_for_delivery',
defaultMessage: 'Out for delivery'
})
case 'Delivered':
return intl.formatMessage({id: 'status_bar.delivered', defaultMessage: 'Delivered'})
default:
return status
}
}
const n = steps.length
const svgWidth = 1080
const svgHeight = 50
const chevronWidth = 24
const radius = 25
// Dynamically calculate step width so all steps + chevrons fit in svgWidth
const stepWidth = (svgWidth + (n - 1) * chevronWidth) / n
let currentStep = steps.findIndex((step) => step === currentStepLabel)
if (currentStep === -1) currentStep = 0
// Helper to get the x offset for each step (overlap chevrons)
const getStepOffset = (i) => i * (stepWidth - chevronWidth)
// Generate polygons/paths for each step
const renderStepShapes = () => {
const shapes = []
for (let i = 0; i < n; i++) {
const x = getStepOffset(i)
let path, stepFill
if (i === 0) {
// First: rounded left, chevron right (chevron tip overlaps next step)
path = `M ${x + radius},0
L ${x + stepWidth - chevronWidth},0
L ${x + stepWidth},${svgHeight / 2}
L ${x + stepWidth - chevronWidth},${svgHeight}
L ${x + radius},${svgHeight}
A ${radius},${radius} 0 0 1 ${x},${svgHeight / 2}
A ${radius},${radius} 0 0 1 ${x + radius},0
Z`
} else if (i === n - 1) {
// Last: chevron left, rounded right (no chevron tip on right)
path = `M ${x},0
L ${x + stepWidth - radius},0
A ${radius},${radius} 0 0 1 ${x + stepWidth},${svgHeight / 2}
A ${radius},${radius} 0 0 1 ${x + stepWidth - radius},${svgHeight}
L ${x},${svgHeight}
L ${x + chevronWidth},${svgHeight / 2}
Z`
} else {
// Middle: chevron left, chevron right (chevron tip overlaps next step)
path = `M ${x},0
L ${x + stepWidth - chevronWidth},0
L ${x + stepWidth},${svgHeight / 2}
L ${x + stepWidth - chevronWidth},${svgHeight}
L ${x},${svgHeight}
L ${x + chevronWidth},${svgHeight / 2}
Z`
}
// Color logic: completed steps = light teal, current step = dark blue, future steps = gray
if (i < currentStep) {
stepFill = theme.colors.teal[100]
} else if (i === currentStep) {
stepFill = theme.colors.blue[900]
} else {
stepFill = theme.colors.gray[200]
}
shapes.push(<path key={i} d={path} fill={stepFill} stroke="white" strokeWidth="2" />)
}
return shapes
}
// Overlay text for each step (shift overlays for overlap)
const renderStepLabels = () => {
const labels = []
for (let i = 0; i < n; i++) {
const x = getStepOffset(i)
// Text color logic: completed steps = dark text, current step = white, future steps = dark text
let labelColor
if (i < currentStep) {
labelColor = theme.colors.black[600]
} else if (i === currentStep) {
labelColor = 'white'
} else {
labelColor = theme.colors.black[600]
}
labels.push(
<Box
key={i}
position="absolute"
top={0}
left={`calc(${(x / svgWidth) * 100}% )`}
width={`calc(${(stepWidth / svgWidth) * 100}% )`}
height="100%"
display="flex"
alignItems="center"
justifyContent="center"
pointerEvents="none"
px={[1, 2]} // Add horizontal padding for text
>
<Text
color={labelColor}
fontWeight="medium"
fontSize={['xs', 'sm', 'md', 'lg']}
textAlign="center"
lineHeight="1.2"
wordBreak="break-word"
hyphens="auto"
maxW="100%"
>
{getLocalizedMessage(steps[i])}
</Text>
</Box>
)
}
return labels
}
return (
<Box position="relative" width="100%" maxWidth="1080px" height="50px">
<svg
width="100%"
height="auto"
viewBox={`0 0 ${svgWidth} ${svgHeight}`}
style={{display: 'block'}}
preserveAspectRatio="none"
>
{renderStepShapes()}
</svg>
{renderStepLabels()}
</Box>
)
}
OrderStatusBar.propTypes = {
currentStepLabel: PropTypes.string
}
export default OrderStatusBar