Skip to content

Commit

Permalink
New components Table, Collapsible and refactoring (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Gaudin authored Apr 25, 2024
2 parents 782ff9b + c6ae375 commit 0104024
Show file tree
Hide file tree
Showing 32 changed files with 768 additions and 311 deletions.
3 changes: 2 additions & 1 deletion apps/docs/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"IconSpecTable": true,
"PreviewComponent": true,
"MigrateGuide": true,
"PackageInstallation": true
"PackageInstallation": true,
"PropTable": true
},
"overrides": [
{
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/app/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ html {
}

main {
padding-block: var(--hd-space-4) var(--hd-space-8);
padding-block: var(--hd-space-8);
flex: 1 1 auto;
min-width: 0;
order: 1;
Expand Down
1 change: 1 addition & 0 deletions apps/docs/app/lib/getComponentDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function getMDXData(dir: string) {
const { frontmatter, content } = await readMDXFile(path.join(dir, file));
const slug = path.basename(file, path.extname(file));


return {
slug,
frontmatter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

.hd-component-wrapper__action {
position: absolute;
inset: 1rem 1rem 0 auto;
inset: 1rem 1rem auto auto;
z-index: 100;
}

Expand Down
93 changes: 93 additions & 0 deletions apps/docs/app/ui/components/propTable/PropTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { promises as fs } from "fs";
import path from "path";
import type { PropItem } from "react-docgen-typescript/lib/parser";
import type { ComponentDocWithGroups } from "@/scripts/generateComponentData.mjs";
import Collapsible from "@/components/collapsible/Collapsible.tsx";
import Title from "@/components/title/Title.tsx";

import "./propTable.css";

export interface PropTableProps {
component: string;
}

interface PropItemTypeValue {
name: string;
value?: Array<{ value: string; name: string }>;
raw?: string;
}

const filePath = path.join(process.cwd(), "datas", "components");

const getType = (type: PropItemTypeValue) => {
const handler: {
[key: string]: (type: PropItemTypeValue) => string;
} = {
enum: t =>
t.value ? t.value.map(item => item.value.replace(/'/g, "")).join(" \\| ") : "",
union: t => t.value ? t.value.map(item => item.name).join(" \\| ") : ""
};
if (typeof handler[type.name] === "function") {
return handler[type.name](type).replace(/\|/g, "");
}

return type.name;
};

const renderRow = (key: string, prop: PropItem) => {
const { name, type, defaultValue, required, description } = prop;

return (
<tr key={key}>
<td>{name}</td>
<td>{getType(type)}</td>
<td>{defaultValue?.value}</td>
<td>{required ? "✓" : "✗"}</td>
<td>{description}</td>
</tr>
);
};

export default async function PropTable({ component }: PropTableProps) {
const file = await fs.readFile(filePath + `/${component}.json`, "utf8");
const data = JSON.parse(file);

return data.map((item: ComponentDocWithGroups) => {
const { description, displayName, groups } = item;

return (
<>
<p>{displayName}</p>
<p>{description}</p>
{Object.keys(groups).map(group => {
return (
<Collapsible
key={group}
title={
<Title as="h3" level={3}>
{group}
</Title>
}
className="props__section"
>
<table>
<thead>
<tr>
<th>Property</th>
<th>Type</th>
<th>Default</th>
<th>Required</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{Object.keys(groups[group]).map(key => renderRow(key, groups[group][key]))}
</tbody>
</table>
</Collapsible>
);
})}
</>
);
});
}
7 changes: 7 additions & 0 deletions apps/docs/app/ui/components/propTable/propTable.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.props__section {
margin-top: var(--hd-space-1);
}

.props__section .hd-collapsible__content {
margin-top: var(--hd-space-1);
}
4 changes: 2 additions & 2 deletions apps/docs/app/ui/layout/aside/aside.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
/* making sure that if a user toggles the list and resize his browser the list is still visible */

/* We could do this in JS but I think that fixes the issue */
display: flex!important;
display: flex !important;
}

.hd-aside__list--closed {
Expand Down Expand Up @@ -117,7 +117,7 @@
left: 0;
z-index: 1;
border-radius: var(--hd-space-1);
transition: top .25s cubic-bezier(0,1,.5,1);
transition: top .25s cubic-bezier(0, 1, .5, 1);
display: none;
}

Expand Down
6 changes: 4 additions & 2 deletions apps/docs/app/ui/tokens/preview/preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
width: var(--hd-space-5);
height: var(--hd-space-3);
border-radius: 0;
float: right;

/* float: right; */
display: inline-block;
background-color: var(--hd-color-primary-surface);
}

Expand All @@ -18,7 +20,7 @@
.hd-preview--font {
align-items: center;
background-color: transparent;
display: flex;
display: inline-flex;
font-size: 1.5rem;
height: auto;
justify-content: flex-end;
Expand Down
39 changes: 7 additions & 32 deletions apps/docs/app/ui/tokens/table/IconSpecTable.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use client";
import Table from "@/components/table/Table";

import { Cell, Column, Row, Table as TableRA, TableBody, TableHeader } from "react-aria-components";

import "./table.css";
import "./tokenTable.css";

interface IconSpecTableProps {
data: {
Expand All @@ -15,34 +13,11 @@ interface IconSpecTableProps {
}

const IconSpecTable = ({ data }: IconSpecTableProps) => {
const sizes = ["sm", "md", "lg"];

const listItems = data?.map(row => {
return (
<Row key={row.name} className="hd-table__row">
<Cell className="hd-table__cell">{row.name}</Cell>
{sizes.map(size => (
<Cell key={size} className="hd-table__cell">
{row[size]}
</Cell>
))}
</Row>
);
});

return (
<TableRA className="hd-table hd-table--icon-spec" aria-label="Tokens">
<TableHeader>
<Column className="hd-table__column" isRowHeader>Anatomy</Column>
<Column className="hd-table__column">Small</Column>
<Column className="hd-table__column">Medium</Column>
<Column className="hd-table__column">Large</Column>
</TableHeader>
<TableBody>
{listItems}
</TableBody>
</TableRA>
);
return <Table className="hd-table--icon-spec"
head={["Anatomy", "Small", "Medium", "Large"]}
data={data}
ariaLabel="Icons specs"
/>;
};

export default IconSpecTable;
50 changes: 0 additions & 50 deletions apps/docs/app/ui/tokens/table/Table.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Meta, StoryObj } from "@storybook/react";

import Table from "./Table";
import TokenTable from "./TokenTable.tsx";

const meta = {
title: "app/tokens/Table",
component: Table
} satisfies Meta<typeof Table>;
title: "app/tokens/TokenTable",
component: TokenTable
} satisfies Meta<typeof TokenTable>;

export default meta;
type Story = StoryObj<typeof meta>;
Expand Down
36 changes: 36 additions & 0 deletions apps/docs/app/ui/tokens/table/TokenTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Table from "@/components/table/Table";

import Preview from "@/app/ui/tokens/preview/Preview";
import Code from "@/components/code/Code";

import "./tokenTable.css";

interface TableProps {
category: string;
noPreview?: boolean;
data: {
name: string;
value: string;
}[];
}

const TokenTable = ({ category, data, noPreview }: TableProps) => {
const formattedData = data.map(token => {
const { name, value } = token;

return {
name: <Code value={`--${name}`}>{`--${name}`}</Code>,
value: value,
preview: !noPreview && <Preview value={value} name={name} category={category} />
};
});


return <Table head={["Name", "Value", !noPreview && "Preview"]}
data={formattedData}
lastColumnAlignment="right"
ariaLabel="Tokens"
/>;
};

export default TokenTable;
Loading

0 comments on commit 0104024

Please sign in to comment.