Skip to content

Commit 037a6b2

Browse files
juah255claudekzamanbd
authored
Use theme-aware background in DataViews (#74)
* Use theme-aware background in DataViews and SkeletonTable Replace hardcoded bg-white with bg-background CSS variable so DataViews table and skeleton loader respect the active theme. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Refactor PosterGrid component and enhance theme-aware background styles in DataViews * Enhance user data generation with first and last names, improve SkeletonTable rendering for grid and list views, and add vertical alignment to table cells --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Kamruzzaman <kzamanbn@gmail.com>
1 parent 7806051 commit 037a6b2

3 files changed

Lines changed: 87 additions & 84 deletions

File tree

src/components/wordpress/DataViews.stories.tsx

Lines changed: 35 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,35 @@ interface User {
1717
}
1818

1919
// Generate sample data
20+
const firstNames = [
21+
"James", "Emma", "Liam", "Olivia", "Noah", "Ava", "William", "Sophia",
22+
"Benjamin", "Isabella", "Lucas", "Mia", "Henry", "Charlotte", "Alexander",
23+
"Amelia", "Daniel", "Harper", "Matthew", "Evelyn", "Sebastian", "Aria",
24+
"Jack", "Chloe", "Owen",
25+
];
26+
const lastNames = [
27+
"Anderson", "Martinez", "Thompson", "Garcia", "Robinson", "Clark", "Lewis",
28+
"Walker", "Hall", "Young", "King", "Wright", "Lopez", "Hill", "Scott",
29+
"Green", "Adams", "Baker", "Nelson", "Carter", "Mitchell", "Perez",
30+
"Roberts", "Turner", "Phillips",
31+
];
32+
2033
const generateUsers = (count: number): User[] => {
2134
const statuses: User["status"][] = ["active", "inactive", "pending"];
2235
const roles = ["Admin", "Editor", "Viewer", "Manager"];
23-
24-
return Array.from({ length: count }, (_, i) => ({
25-
id: `user-${i + 1}`,
26-
name: `User ${i + 1}`,
27-
email: `user${i + 1}@example.com`,
28-
status: statuses[i % 3],
29-
role: roles[i % 4],
30-
joinedAt: new Date(2024, i % 12, (i % 28) + 1).toLocaleDateString(),
31-
}));
36+
37+
return Array.from({ length: count }, (_, i) => {
38+
const first = firstNames[i % firstNames.length];
39+
const last = lastNames[i % lastNames.length];
40+
return {
41+
id: `user-${i + 1}`,
42+
name: `${first} ${last}`,
43+
email: `${first.toLowerCase()}.${last.toLowerCase()}@example.com`,
44+
status: statuses[i % 3],
45+
role: roles[i % 4],
46+
joinedAt: new Date(2024, i % 12, (i % 28) + 1).toLocaleDateString(),
47+
};
48+
});
3249
};
3350

3451
const allUsers = generateUsers(100);
@@ -959,80 +976,24 @@ FullFeatured.storyName = "Full Featured";
959976
*/
960977
function PosterGrid({ items }: { items: User[] }) {
961978
return (
962-
<div
963-
style={{
964-
display: "grid",
965-
gridTemplateColumns: "repeat(auto-fill, minmax(280px, 1fr))",
966-
gap: "16px",
967-
padding: "16px 0",
968-
}}
969-
>
979+
<div className="grid grid-cols-[repeat(auto-fill,minmax(280px,1fr))] gap-4 py-4">
970980
{items.map((item) => (
971981
<div
972982
key={item.id}
973-
style={{
974-
position: "relative",
975-
aspectRatio: "4 / 3",
976-
borderRadius: "8px",
977-
overflow: "hidden",
978-
backgroundColor: "#cbd5e1", // Placeholder background
979-
}}
983+
className="relative aspect-4/3 rounded-lg overflow-hidden bg-slate-300"
980984
>
981-
<div
982-
style={{
983-
position: "absolute",
984-
bottom: 0,
985-
left: 0,
986-
right: 0,
987-
padding: "48px 16px 16px",
988-
background:
989-
"linear-gradient(to top, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0.4) 60%, transparent 100%)",
990-
color: "white",
991-
}}
992-
>
993-
<h3
994-
style={{
995-
margin: "0 0 4px",
996-
fontSize: "18px",
997-
fontWeight: 600,
998-
textShadow: "0 1px 2px rgba(0,0,0,0.5)",
999-
}}
1000-
>
985+
<div className="absolute bottom-0 left-0 right-0 pt-12 px-4 pb-4 bg-linear-to-t from-black/80 via-black/40 to-transparent text-white">
986+
<h3 className="mb-1 text-lg font-semibold drop-shadow-[0_1px_2px_rgba(0,0,0,0.5)]">
1001987
{item.name}
1002988
</h3>
1003-
<p
1004-
style={{
1005-
margin: "0 0 8px",
1006-
fontSize: "13px",
1007-
opacity: 0.9,
1008-
overflow: "hidden",
1009-
textOverflow: "ellipsis",
1010-
whiteSpace: "nowrap",
1011-
}}
1012-
>
989+
<p className="mb-2 text-[13px] opacity-90 overflow-hidden text-ellipsis whitespace-nowrap">
1013990
{item.email}
1014991
</p>
1015-
<div style={{ display: "flex", gap: "6px" }}>
1016-
<span
1017-
style={{
1018-
fontSize: "11px",
1019-
padding: "2px 8px",
1020-
borderRadius: "4px",
1021-
backgroundColor: "rgba(255,255,255,0.2)",
1022-
textTransform: "capitalize",
1023-
}}
1024-
>
992+
<div className="flex gap-1.5">
993+
<span className="text-[11px] px-2 py-0.5 rounded bg-white/20 capitalize">
1025994
{item.role}
1026995
</span>
1027-
<span
1028-
style={{
1029-
fontSize: "11px",
1030-
padding: "2px 8px",
1031-
borderRadius: "4px",
1032-
backgroundColor: "rgba(255,255,255,0.2)",
1033-
textTransform: "capitalize",
1034-
}}
1035-
>
996+
<span className="text-[11px] px-2 py-0.5 rounded bg-white/20 capitalize">
1036997
{item.status}
1037998
</span>
1038999
</div>
@@ -1083,9 +1044,8 @@ export const LayoutCustomComponent: StoryFn = () => {
10831044
view={view}
10841045
fields={fields}
10851046
onChangeView={setView}
1086-
defaultLayouts={{ table: {} }}
10871047
>
1088-
<div style={{ padding: "2px" }}>
1048+
<div className="p-0.5">
10891049
{DataViews.Search && <DataViews.Search />}
10901050
<PosterGrid items={paginatedData} />
10911051
</div>

src/components/wordpress/dataviews.tsx

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ const FilterItems = ({
228228

229229
return (
230230
<Fragment>
231-
<div className={cn('flex w-full justify-between items-center', className)}>
231+
<div className={cn('sm:flex w-full justify-between items-center', className)}>
232232
<div className="flex flex-row flex-wrap gap-4 items-center">
233233
{activeFilters.map((id) => {
234234
const field = fields.find((f) => f.id === id);
@@ -368,27 +368,63 @@ const ListEmpty = ({ icon, description, title }: ListEmptyProps) => {
368368
};
369369

370370
/**
371-
* Renders a skeleton table with real column headers and animated placeholder rows.
371+
* Renders a skeleton loading state matching the current view type.
372372
*/
373373
function SkeletonTable({
374374
rows,
375375
headers,
376376
hasActions,
377-
hasBulkActions
377+
hasBulkActions,
378+
viewType = 'table'
378379
}: {
379380
rows: number;
380381
headers: string[];
381382
hasActions: boolean;
382383
hasBulkActions: boolean;
384+
viewType?: string;
383385
}) {
386+
if (viewType === 'grid') {
387+
return (
388+
<div className="grid grid-cols-[repeat(auto-fill,minmax(260px,1fr))] gap-4 p-4">
389+
{Array.from({ length: rows }, (_, i) => (
390+
<div key={i} className="rounded-lg border border-border bg-background p-4 space-y-3">
391+
<Skeleton className="aspect-video w-full rounded-md" />
392+
<Skeleton className="h-4 w-3/4" />
393+
<Skeleton className="h-3 w-1/2" />
394+
<div className="flex gap-2 pt-1">
395+
<Skeleton className="h-5 w-14 rounded-full" />
396+
<Skeleton className="h-5 w-14 rounded-full" />
397+
</div>
398+
</div>
399+
))}
400+
</div>
401+
);
402+
}
403+
404+
if (viewType === 'list') {
405+
return (
406+
<div className="divide-y divide-border">
407+
{Array.from({ length: rows }, (_, i) => (
408+
<div key={i} className="flex items-center gap-4 px-5 py-4">
409+
<div className="flex-1 space-y-2">
410+
<Skeleton className="h-4 w-1/3" />
411+
<Skeleton className="h-3 w-1/2" />
412+
</div>
413+
{hasActions && <Skeleton className="h-4 w-8 shrink-0" />}
414+
</div>
415+
))}
416+
</div>
417+
);
418+
}
419+
384420
const widths = ['w-3/4', 'w-1/2', 'w-2/3', 'w-5/6', 'w-2/5'];
385421

386422
return (
387423
<table className="w-full text-sm border-collapse">
388424
<thead>
389-
<tr className="border-b border-border">
425+
<tr className="bg-background border-b border-border">
390426
{hasBulkActions && (
391-
<th className="h-12 bg-white w-12 px-5 align-middle">
427+
<th className="h-12 bg-background w-12 px-5 align-middle">
392428
<Skeleton className="h-4 w-4 rounded-sm" />
393429
</th>
394430
)}
@@ -398,7 +434,7 @@ function SkeletonTable({
398434
<th
399435
key={colIdx}
400436
className={cn(
401-
'h-12 bg-white px-5 align-middle text-[11px] font-medium text-foreground uppercase tracking-normal',
437+
'h-12 bg-background px-5 align-middle text-[11px] font-medium text-foreground uppercase tracking-normal',
402438
isActions ? 'text-right' : 'text-left'
403439
)}>
404440
{label}
@@ -409,7 +445,7 @@ function SkeletonTable({
409445
</thead>
410446
<tbody>
411447
{Array.from({ length: rows }, (_, rowIdx) => (
412-
<tr key={rowIdx} className="border-b border-border last:border-b-0">
448+
<tr key={rowIdx} className="bg-background border-b border-border last:border-b-0">
413449
{hasBulkActions && (
414450
<td className="h-12 w-12 px-5 align-middle">
415451
<Skeleton className="h-4 w-4 rounded-sm" />
@@ -710,7 +746,7 @@ export function DataViews<Item>(props: DataViewsProps<Item>) {
710746
{header && <div className="font-semibold text-sm text-foreground">{header}</div>}
711747
<div
712748
className={cn(
713-
'flex gap-2 md:flex-row flex-col justify-between w-full items-center',
749+
'flex gap-2 md:flex-row flex-col justify-between w-full',
714750
(tabItems.length || search || headerContent.length) &&
715751
'border-b border-border p-4 md:px-4 md:py-0'
716752
)}>
@@ -796,6 +832,7 @@ export function DataViews<Item>(props: DataViewsProps<Item>) {
796832
)}
797833
{isLoading ? (
798834
<SkeletonTable
835+
viewType={view.type}
799836
rows={viewPerPageValue}
800837
hasActions={!!props.actions?.length}
801838
hasBulkActions={!!props.onChangeSelection}

src/components/wordpress/style.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
@import "@wordpress/dataviews/build-style/style.css";
44
@import '@wordpress/theme/design-tokens.css';
55

6+
.pui-root-dataviews,
7+
.pui-root-dataviews .dataviews-wrapper {
8+
background-color: var(--background, #FFFFFF);
9+
}
610
.pui-root-dataviews:not(.custom-layout) {
711
border-radius: 6px;
812
border: 1px solid var(--border, #E7E7E7);
913
box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 0.1019607843);
10-
background-color: var(--background, #FFFFFF);
1114
color: var(--foreground, #1D1D1D);
1215
}
1316
.pui-root-dataviews .dataviews-no-results {
1417
border-top: 1px solid var(--border, #E7E7E7);
18+
background-color: var(--background, #FFFFFF);
1519
}
1620
.pui-root-dataviews:not(.custom-layout) .dataviews-wrapper {
1721
border-radius: 6px;
@@ -46,6 +50,7 @@
4650
color: var(--foreground, #1D1D1D);
4751
border-bottom-color: var(--border, #E7E7E7);
4852
background-color: var(--background, #FFFFFF);
53+
vertical-align: middle;
4954
}
5055
.pui-root-dataviews .dataviews-wrapper .dataviews-view-table td,
5156
.pui-root-dataviews .dataviews-wrapper .dataviews-view-table th {
@@ -65,6 +70,7 @@
6570
display: flex;
6671
justify-content: space-between !important;
6772
padding: 16px;
73+
background-color: var(--background, #FFFFFF);
6874
}
6975
.pui-root-dataviews .dataviews-wrapper .dataviews-view-table tr td:first-child,
7076
.pui-root-dataviews .dataviews-wrapper .dataviews-view-table tr th:first-child {

0 commit comments

Comments
 (0)