Skip to content

Commit 008a0e3

Browse files
committed
fix lazy loading and skeleton state
1 parent 0974576 commit 008a0e3

File tree

4 files changed

+243
-170
lines changed

4 files changed

+243
-170
lines changed

packages/dev/s2-docs/src/ColorSearchView.tsx

Lines changed: 11 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,17 @@
11
'use client';
22

3-
import {Badge, Content, Heading, IllustratedMessage, pressScale, Skeleton, Text} from '@react-spectrum/s2';
3+
import {Badge, Content, Heading, IllustratedMessage, pressScale, Text} from '@react-spectrum/s2';
44
import Checkmark from '@react-spectrum/s2/icons/Checkmark';
55
import CheckmarkCircle from '@react-spectrum/s2/icons/CheckmarkCircle';
6-
import {colorSwatch, getColorHexMap, getColorScale} from './color.macro' with {type: 'macro'};
6+
import {colorSwatch, getColorScale} from './color.macro' with {type: 'macro'};
77
import {focusRing, iconStyle, style} from '@react-spectrum/s2/style' with {type: 'macro'};
88
import {Header, ListBox, ListBoxItem, ListBoxSection} from 'react-aria-components';
99
import InfoCircle from '@react-spectrum/s2/icons/InfoCircle';
1010
// eslint-disable-next-line monorepo/no-internal-import
1111
import NoSearchResults from '@react-spectrum/s2/illustrations/linear/NoSearchResults';
12-
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
12+
import React, {useCallback, useEffect, useRef, useState} from 'react';
1313
import Similar from '@react-spectrum/s2/icons/Similar';
1414

15-
export const colorHexMaps = getColorHexMap();
16-
17-
const backgroundColors = [
18-
'base', 'layer-1', 'layer-2', 'pasteboard', 'elevated',
19-
'accent', 'accent-subtle', 'neutral', 'neutral-subdued', 'neutral-subtle',
20-
'negative', 'negative-subtle', 'informative', 'informative-subtle',
21-
'positive', 'positive-subtle', 'notice', 'notice-subtle',
22-
'gray', 'gray-subtle', 'red', 'red-subtle', 'orange', 'orange-subtle',
23-
'yellow', 'yellow-subtle', 'chartreuse', 'chartreuse-subtle',
24-
'celery', 'celery-subtle', 'green', 'green-subtle', 'seafoam', 'seafoam-subtle',
25-
'cyan', 'cyan-subtle', 'blue', 'blue-subtle', 'indigo', 'indigo-subtle',
26-
'purple', 'purple-subtle', 'fuchsia', 'fuchsia-subtle', 'magenta', 'magenta-subtle',
27-
'pink', 'pink-subtle', 'turquoise', 'turquoise-subtle',
28-
'cinnamon', 'cinnamon-subtle', 'brown', 'brown-subtle',
29-
'silver', 'silver-subtle', 'disabled'
30-
];
31-
32-
const textColors = [
33-
'accent', 'neutral', 'neutral-subdued', 'negative', 'disabled',
34-
'heading', 'title', 'body', 'detail', 'code'
35-
];
36-
37-
const semanticColorRanges: Record<string, number[]> = {
38-
'accent-color': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
39-
'informative-color': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
40-
'negative-color': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
41-
'notice-color': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
42-
'positive-color': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600]
43-
};
44-
45-
const globalColorRanges: Record<string, number[]> = {
46-
'gray': [25, 50, 75, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000],
47-
'blue': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
48-
'red': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
49-
'orange': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
50-
'yellow': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
51-
'chartreuse': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
52-
'celery': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
53-
'green': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
54-
'seafoam': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
55-
'cyan': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
56-
'indigo': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
57-
'purple': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
58-
'fuchsia': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
59-
'magenta': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
60-
'pink': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
61-
'turquoise': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
62-
'brown': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
63-
'silver': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600],
64-
'cinnamon': [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600]
65-
};
66-
67-
const semanticColors = Object.entries(semanticColorRanges).flatMap(([scale, ranges]) =>
68-
ranges.map(value => ({name: `${scale.replace('-color', '')}-${value}`, section: 'Semantic colors', type: 'backgroundColor'}))
69-
);
70-
71-
const globalColors = Object.entries(globalColorRanges).flatMap(([scale, ranges]) =>
72-
ranges.map(value => ({name: `${scale}-${value}`, section: 'Global colors', type: 'backgroundColor'}))
73-
);
74-
75-
export const colorSections = [
76-
{
77-
id: 'background',
78-
name: 'Background colors',
79-
items: backgroundColors.map(name => ({name, section: 'Background colors', type: 'backgroundColor'}))
80-
},
81-
{
82-
id: 'text',
83-
name: 'Text colors',
84-
items: textColors.map(name => ({name, section: 'Text colors', type: 'color'}))
85-
},
86-
{
87-
id: 'semantic',
88-
name: 'Semantic colors',
89-
items: semanticColors
90-
},
91-
{
92-
id: 'global',
93-
name: 'Global colors',
94-
items: globalColors
95-
}
96-
];
97-
9815
const itemStyle = style({
9916
...focusRing(),
10017
display: 'flex',
@@ -151,7 +68,7 @@ const headerStyle = style({
15168
marginBottom: 4
15269
});
15370

154-
const backgroundSwatches = {
71+
const backgroundSwatches: Record<string, string> = {
15572
'base': colorSwatch('base'),
15673
'layer-1': colorSwatch('layer-1'),
15774
'layer-2': colorSwatch('layer-2'),
@@ -211,7 +128,7 @@ const backgroundSwatches = {
211128
'disabled': colorSwatch('disabled')
212129
};
213130

214-
const textSwatches = {
131+
const textSwatches: Record<string, string> = {
215132
'accent': colorSwatch('accent', 'color'),
216133
'neutral': colorSwatch('neutral', 'color'),
217134
'neutral-subdued': colorSwatch('neutral-subdued', 'color'),
@@ -249,7 +166,7 @@ const brownScale = getColorScale('brown');
249166
const silverScale = getColorScale('silver');
250167
const cinnamonScale = getColorScale('cinnamon');
251168

252-
const scaleSwatches = {
169+
const scaleSwatches: Record<string, string> = {
253170
...Object.fromEntries(accentScale),
254171
...Object.fromEntries(informativeScale),
255172
...Object.fromEntries(negativeScale),
@@ -287,7 +204,11 @@ function CopyInfoMessage() {
287204
}
288205

289206
interface ColorSearchViewProps {
290-
filteredItems: typeof colorSections,
207+
filteredItems: Array<{
208+
id: string,
209+
name: string,
210+
items: Array<{name: string, section: string, type: string}>
211+
}>,
291212
/** Names of colors that exactly match the searched hex value. */
292213
exactMatches?: Set<string>,
293214
/** Names of the closest matching colors when no exact matches exist. */
@@ -439,80 +360,3 @@ function ColorItem({item, sectionId, isCopied = false, isBestMatch = false, isEx
439360
</ListBoxItem>
440361
);
441362
}
442-
443-
function SkeletonColorItem({item}: {item: {id: string}}) {
444-
const ref = useRef(null);
445-
446-
return (
447-
<ListBoxItem
448-
id={item.id}
449-
value={item}
450-
textValue="skeleton"
451-
className={itemStyle}
452-
ref={ref}>
453-
<div
454-
className={swatchStyle}
455-
style={{
456-
width: '32px',
457-
height: '32px',
458-
backgroundColor: 'var(--s2-gray-200)'
459-
} as React.CSSProperties} />
460-
<div
461-
className={style({
462-
maxWidth: 'full',
463-
textOverflow: 'ellipsis',
464-
overflow: 'hidden',
465-
whiteSpace: 'nowrap',
466-
textAlign: 'center'
467-
})}>
468-
<Text styles={style({font: 'ui-sm'})}>Name</Text>
469-
</div>
470-
</ListBoxItem>
471-
);
472-
}
473-
474-
export function ColorSearchSkeleton() {
475-
const mockSections = useMemo(() => [
476-
{
477-
id: 'background',
478-
name: 'Background colors',
479-
items: Array.from({length: 20}, (_, i) => ({id: `skeleton-background-${i}`}))
480-
},
481-
{
482-
id: 'text',
483-
name: 'Text colors',
484-
items: Array.from({length: 10}, (_, i) => ({id: `skeleton-text-${i}`}))
485-
},
486-
{
487-
id: 'semantic',
488-
name: 'Semantic colors',
489-
items: Array.from({length: 30}, (_, i) => ({id: `skeleton-semantic-${i}`}))
490-
},
491-
{
492-
id: 'global',
493-
name: 'Global colors',
494-
items: Array.from({length: 40}, (_, i) => ({id: `skeleton-global-${i}`}))
495-
}
496-
], []);
497-
498-
return (
499-
<Skeleton isLoading>
500-
<div className={style({display: 'flex', flexDirection: 'column', gap: 8})}>
501-
<ListBox
502-
aria-label="Colors loading"
503-
layout="grid"
504-
className={listBoxStyle}
505-
items={mockSections}>
506-
{section => (
507-
<ListBoxSection id={section.id} className={sectionStyle}>
508-
<Header className={headerStyle}>{section.name}</Header>
509-
{section.items.map(item => (
510-
<SkeletonColorItem key={item.id} item={item} />
511-
))}
512-
</ListBoxSection>
513-
)}
514-
</ListBox>
515-
</div>
516-
</Skeleton>
517-
);
518-
}

packages/dev/s2-docs/src/SearchMenu.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import {ActionButton, SearchField} from '@react-spectrum/s2';
44
import {Autocomplete, Dialog, Key, OverlayTriggerStateContext, Provider} from 'react-aria-components';
55
import Close from '@react-spectrum/s2/icons/Close';
6-
import {ColorSearchSkeleton, ColorSearchView} from './ColorSearchView';
6+
import {ColorSearchSkeleton} from './colorSearchData';
77
import {ComponentCardView} from './ComponentCardView';
88
import {
99
getResourceTags,
10+
LazyColorSearchView,
1011
LazyIconSearchView,
1112
SearchEmptyState,
1213
useFilteredColors,
@@ -196,7 +197,7 @@ export function SearchMenu(props: SearchMenuProps) {
196197
{selectedTagId === 'colors' && (
197198
<div className={style({flexGrow: 1, overflow: 'auto', paddingX: 16, paddingBottom: 16})}>
198199
<Suspense fallback={<ColorSearchSkeleton />}>
199-
<ColorSearchView filteredItems={filteredColors.sections} exactMatches={filteredColors.exactMatches} closestMatches={filteredColors.closestMatches} />
200+
<LazyColorSearchView filteredItems={filteredColors.sections} exactMatches={filteredColors.exactMatches} closestMatches={filteredColors.closestMatches} />
200201
</Suspense>
201202
</div>
202203
)}

0 commit comments

Comments
 (0)