Skip to content

Commit a252b07

Browse files
authored
Merge pull request #606 from devtron-labs/feat/rb-keyboard-nav
feat: add useStickyEvent hook
2 parents ff7f0c0 + 77d001d commit a252b07

File tree

13 files changed

+229
-24
lines changed

13 files changed

+229
-24
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.8.9",
3+
"version": "1.8.10",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/SegmentedBarChart/SegmentedBarChart.tsx

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,71 @@ import { FALLBACK_ENTITY } from './constants'
2121
import './styles.scss'
2222

2323
const SegmentedBarChart: React.FC<SegmentedBarChartProps> = ({
24-
entities = [FALLBACK_ENTITY],
24+
entities: userEntities = [FALLBACK_ENTITY],
2525
rootClassName,
2626
countClassName,
2727
labelClassName,
2828
isProportional,
2929
swapLegendAndBar = false,
3030
showAnimationOnBar = false,
31+
isLoading,
3132
}) => {
33+
const entities = isLoading ? [FALLBACK_ENTITY] : userEntities
3234
const total = entities.reduce((sum, entity) => entity.value + sum, 0)
3335
const filteredEntities = entities.filter((entity) => !!entity.value)
3436

3537
const calcSegmentWidth = (entity: Entity) => `${(entity.value / total) * 100}%`
3638

37-
const renderLabel = (label: string) => (
38-
<span className={labelClassName} data-testid={`segmented-bar-chart-${label}-label`}>
39-
{label}
40-
</span>
41-
)
39+
const renderLabel = (label: Entity['label']) =>
40+
isLoading ? (
41+
<div className="shimmer w-120" />
42+
) : (
43+
<span className={labelClassName} data-testid={`segmented-bar-chart-${label}-label`}>
44+
{label}
45+
</span>
46+
)
4247

43-
const renderValue = (value: string | number, label: string) => (
44-
<span className={countClassName} data-testid={`segmented-bar-chart-${label}-value`}>
45-
{isProportional ? `${value}/${total}` : value}
46-
</span>
47-
)
48+
const renderValue = (value: Entity['value'], label: Entity['label']) =>
49+
isLoading ? (
50+
<div className="shimmer w-64 lh-1-5 h-24" />
51+
) : (
52+
<span className={countClassName} data-testid={`segmented-bar-chart-${label}-value`}>
53+
{isProportional ? `${value}/${total}` : value}
54+
</span>
55+
)
4856

4957
const renderContent = () => {
5058
if (isProportional) {
5159
return filteredEntities.map((entity, idx) => (
5260
// eslint-disable-next-line react/no-array-index-key
53-
<div key={idx} className="flexbox-col">
61+
<div key={idx} className={`flexbox-col ${isLoading ? 'dc__gap-10' : ''}`}>
5462
{renderValue(entity.value, entity.label)}
63+
5564
<div className="flex left dc__gap-6">
56-
<span style={{ backgroundColor: entity.color }} className="h-12 dc__border-radius-2 w-4" />
65+
{!isLoading && (
66+
<span style={{ backgroundColor: entity.color }} className="h-12 dc__border-radius-2 w-4" />
67+
)}
68+
5769
{renderLabel(entity.label)}
5870
</div>
5971
</div>
6072
))
6173
}
74+
6275
return entities.map((entity, idx) => (
6376
// eslint-disable-next-line react/no-array-index-key
6477
<div key={idx} className="flexbox dc__gap-4 dc__align-items-center">
65-
<div className="dot" style={{ backgroundColor: entity.color, width: '10px', height: '10px' }} />
78+
{!isLoading && (
79+
<div className="dot" style={{ backgroundColor: entity.color, width: '10px', height: '10px' }} />
80+
)}
81+
6682
{renderValue(entity.value, entity.label)}
83+
6784
{renderLabel(entity.label)}
6885
</div>
6986
))
7087
}
7188

72-
if (!entities.length) {
73-
return null
74-
}
75-
7689
const renderLegend = () => (
7790
<div className={`flexbox flex-wrap dc__row-gap-4 ${isProportional ? 'dc__gap-24' : 'dc__gap-16'}`}>
7891
{renderContent()}
@@ -92,16 +105,21 @@ const SegmentedBarChart: React.FC<SegmentedBarChartProps> = ({
92105
>
93106
{filteredEntities.map((entity, index, map) => (
94107
<div
95-
key={entity.label}
108+
// eslint-disable-next-line react/no-array-index-key
109+
key={index}
96110
className={`h-8 ${index === 0 ? 'dc__left-radius-4' : ''} ${
97111
index === map.length - 1 ? 'dc__right-radius-4' : ''
98-
}`}
112+
} ${isLoading ? 'shimmer' : ''}`}
99113
style={{ backgroundColor: entity.color, width: calcSegmentWidth(entity) }}
100114
/>
101115
))}
102116
</motion.div>
103117
)
104118

119+
if (!entities.length) {
120+
return null
121+
}
122+
105123
return (
106124
<div className={`flexbox-col w-100 dc__gap-12 ${rootClassName}`}>
107125
{swapLegendAndBar ? (

src/Common/SegmentedBarChart/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
export const FALLBACK_ENTITY = {
1818
color: 'var(--N300)',
1919
label: '-',
20-
value: 0,
20+
value: 1,
2121
}

src/Common/SegmentedBarChart/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ export interface SegmentedBarChartProps {
2828
isProportional?: boolean
2929
swapLegendAndBar?: boolean
3030
showAnimationOnBar?: boolean
31+
isLoading?: boolean
3132
}

src/Shared/Helpers.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,3 +1062,6 @@ export const deriveBorderRadiusAndBorderClassFromConfig = ({
10621062
const { top, right, bottom, left } = getSanitizedBorderConfig(borderRadiusConfig)
10631063
return `${deriveBorderClassFromConfig(borderConfig)} ${deriveBorderRadiusClassFromConfig({ top, right, bottom, left })}`
10641064
}
1065+
1066+
export const getClassNameForStickyHeaderWithShadow = (isStuck: boolean, topClassName = 'dc__top-0') =>
1067+
`dc__position-sticky ${topClassName} dc__transition--box-shadow ${isStuck ? 'dc__box-shadow--header' : ''}`

src/Shared/Hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export * from './UsePrompt'
1818
export * from './useGetResourceKindsOptions'
1919
export * from './UseDownload'
2020
export * from './useForm'
21+
export * from './useStickyEvent'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const FALLBACK_SENTINEL_HEIGHT = '1px'
2+
export const OBSERVER_THRESHOLD = 1
3+
export const OBSERVER_ROOT_MARGIN = '0px'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as useStickyEvent } from './useStickyEvent'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.sticky-container__sentinel {
2+
position: absolute;
3+
bottom: 100%;
4+
left: 0;
5+
right: 0;
6+
flex-shrink: 0;
7+
visibility: hidden;
8+
z-index: -1;
9+
}

0 commit comments

Comments
 (0)