Skip to content

Commit

Permalink
feat: resizing in it's own package
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgallo committed Aug 31, 2024
1 parent 2ab4908 commit f74513a
Show file tree
Hide file tree
Showing 19 changed files with 2,825 additions and 1 deletion.
3 changes: 2 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"projects": {
"gallery": "packages/gallery"
"gallery": "packages/gallery",
"resizing": "packages/resizing"
},
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"namedInputs": {
Expand Down
24 changes: 24 additions & 0 deletions packages/resizing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
50 changes: 50 additions & 0 deletions packages/resizing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
- Optionally add `...tseslint.configs.stylisticTypeChecked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:

```js
// eslint.config.js
import react from 'eslint-plugin-react'

export default tseslint.config({
// Set the react version
settings: { react: { version: '18.3' } },
plugins: {
// Add the react plugin
react,
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
```
29 changes: 29 additions & 0 deletions packages/resizing/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
"@typescript-eslint/no-explicit-any": "off",
},
},
)
13 changes: 13 additions & 0 deletions packages/resizing/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions packages/resizing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "resizing",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@carbon/ibm-products": "^2.47.0",
"@carbon/react": "^1.64.1",
"@faker-js/faker": "^8.4.1",
"@tanstack/match-sorter-utils": "^8.19.4",
"@tanstack/react-query": "^5.52.1",
"@tanstack/react-table": "^8.20.1",
"@tanstack/react-virtual": "^3.10.4",
"classnames": "^2.5.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sass": "^1.77.8"
},
"devDependencies": {
"@eslint/js": "^9.9.0",
"@types/node": "^22.5.0",
"@types/react": "^18.3.4",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^9.9.0",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
"eslint-plugin-react-refresh": "^0.4.9",
"globals": "^15.9.0",
"typescript": "^5.5.3",
"typescript-eslint": "^8.0.1",
"vite": "^5.4.1"
}
}
1 change: 1 addition & 0 deletions packages/resizing/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions packages/resizing/src/App.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@use '@carbon/styles/scss/theme' as *;
@use '@carbon/styles/scss/spacing' as *;
@use '@carbon/styles/scss/utilities';
@use '@carbon/styles/scss/colors';
@use '@carbon/styles/scss/type';
@use '@carbon/type/scss/font-family';

.page-grid {
margin-top: 4rem;
}

.flex {
display: flex;
align-items: center;
}

.basic-table th,
.cds--data-table th:last-of-type {
position: relative;
}

.resizer {
position: absolute;
top: 0;
height: 100%;
width: 5px;
background: rgba(0, 0, 0, 0.5);
cursor: col-resize;
user-select: none;
touch-action: none;
}

.resizer.ltr {
right: 0;
}

.resizer.rtl {
left: 0;
}

.resizer.isResizing {
background: blue;
opacity: 1;
}

@media (hover: hover) {
.resizer {
opacity: 0;
}

*:hover > .resizer {
opacity: 1;
}
}

.tanstack-example {
padding-bottom: 2rem;
}
155 changes: 155 additions & 0 deletions packages/resizing/src/ResizableCols.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { useRef, useState, useLayoutEffect } from 'react'
import { DataTable } from '@carbon/react';
const {
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableHeader,
TableRow
} = DataTable;

import {
createColumnHelper,
flexRender,
getCoreRowModel,
useReactTable,
ColumnResizeMode
} from '@tanstack/react-table'
import { makeData } from './makeData';

type Resource = {
id: string
name: string
rule: string
status: string
other: string
example: string
}

const columnHelper = createColumnHelper<Resource>()

const columns = [
columnHelper.accessor(row => row.name, {
id: 'name',
cell: info => <i>{info.getValue()}</i>,
header: () => <span>Name</span>,
}),
columnHelper.accessor('rule', {
header: () => 'Rule',
cell: info => info.renderValue(),
}),
columnHelper.accessor('status', {
header: () => <span>Status</span>,
}),
columnHelper.accessor('other', {
header: 'Other',
}),
columnHelper.accessor('example', {
header: 'Example',
}),
]

export const ResizableCols = () => {
const [data] = useState(makeData(7))

const [columnResizeMode] = useState<ColumnResizeMode>('onChange')

const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
columnResizeMode,
})

const tableWrap = useRef<HTMLDivElement>();

useLayoutEffect(() => {
const tableWrapElement = tableWrap.current;
if (tableWrapElement) {
const tableElement = tableWrapElement.querySelector('table');
tableElement.style.width = `${table.getCenterTotalSize()}px`;
}
}, [table]);

return (
<TableContainer
title="Resizable columns"
className="basic-table tanstack-example"
style={{
width: table.getCenterTotalSize(),
}}
>
<Table
size="lg"
useZebraStyles={false}
aria-label="sample table"
>
<TableHead>
{table.getHeaderGroups().map(headerGroup => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map(header => (
<TableHeader
key={header.id}
style={{
width: header.getSize(),
}}
>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
<div
{...{
onDoubleClick: () => header.column.resetSize(),
onMouseDown: header.getResizeHandler(),
onTouchStart: header.getResizeHandler(),
className: `resizer ${
table.options.columnResizeDirection
} ${
header.column.getIsResizing() ? 'isResizing' : ''
}`,
style: {
transform:
columnResizeMode === 'onEnd' &&
header.column.getIsResizing()
? `translateX(${
(table.options.columnResizeDirection ===
'rtl'
? -1
: 1) *
(table.getState().columnSizingInfo
.deltaOffset ?? 0)
}px)`
: '',
},
}}
/>
</TableHeader>
))}
</TableRow>
))}
</TableHead>
<TableBody>
{table.getRowModel().rows.map(row => (
<TableRow key={row.id}>
{row.getVisibleCells().map(cell => (
<TableCell
key={cell.id}
style={{
width: cell.column.getSize(),
}}
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
)
}
Loading

0 comments on commit f74513a

Please sign in to comment.