CsvViewer

Open a CSV as a table: typed columns, search, sort, filters from the data, column controls and export of the filtered rows.

Status: beta. Category: Viewers.

import { CsvViewer } from '@glyphui/viewers/csv'

Exports: CsvViewer, csvViewerStyles, parseCsv, buildTable, inferColumnType.

Notes

  • Install the optional peer dependencies first: npm install papaparse @tanstack/react-table @tanstack/react-virtual.
  • Quotes, escaped quotes, line breaks inside quoted cells and a byte-order mark are all handled. Rows that can’t be read cleanly are listed with their row numbers.
  • A column is typed when 95% of its values agree, so one “n/a” doesn’t turn numbers into text. Columns with a few repeating values get a multi-select filter.
  • The header row is detected; people can turn it off, and change the delimiter, from the toolbar.

CsvViewer props

PropTypeDefaultDescription
classNamestring
contentstringCSV text, instead of source.
delimiterstringStarting delimiter. Detected by default.
fileNamestringName used for the export file and the table label.
headerboolean | "auto"Whether the first row names the columns. auto (default) guesses.
heightstring | numberHeight of the table area. Default 480.
maxRowsnumberStops reading after this many rows. Default 1,000,000.
onParsed((result: CsvParseResult) => void)Called after each successful parse.
sourceViewerSourceThe CSV file: a URL, a File/Blob, or bytes.

Example: Customers

Numbers, dates and yes/no columns are detected, each with a matching filter.

import { CsvViewer } from '@glyphui/viewers/csv'
import { fixture } from '../../../lib/fixtures'

export default function Example() {
  return <CsvViewer source={fixture('customers.csv')} height={420} className="w-full" />
}

Example: 50,000 rows

Big files parse in a worker with progress and render only the rows in view.

import { CsvViewer } from '@glyphui/viewers/csv'
import { useMemo } from 'react'

const regions = ['North America', 'Europe', 'Asia Pacific', 'Latin America']

/** 50,000 rows of order data, generated in the browser. */
function generate(rows: number): string {
  const lines = ['order_id,region,amount,shipped,created']
  for (let i = 0; i < rows; i++) {
    const day = String((i % 28) + 1).padStart(2, '0')
    const month = String((i % 12) + 1).padStart(2, '0')
    lines.push(
      `${100000 + i},${regions[i % 4]},${((i * 7919) % 50000) / 100},${i % 3 !== 0},2026-${month}-${day}`,
    )
  }
  return lines.join('\n')
}

export default function Example() {
  const csv = useMemo(() => generate(50_000), [])
  return <CsvViewer content={csv} fileName="orders.csv" height={420} className="w-full" />
}

Example: Pasted text

Semicolon-separated data, detected automatically.

import { Field, Textarea } from '@glyphui/ui'
import { CsvViewer } from '@glyphui/viewers/csv'
import { useState } from 'react'

const sample = `country;capital;population_millions;eu_member
France;Paris;68.4;true
Germany;Berlin;84.5;true
Norway;Oslo;5.5;false
Spain;Madrid;48.6;true
Switzerland;Bern;8.9;false`

export default function Example() {
  const [text, setText] = useState(sample)
  return (
    <div className="grid w-full gap-4">
      <Field label="Paste CSV">
        <Textarea
          rows={4}
          value={text}
          onChange={(event) => setText(event.target.value)}
          className="font-mono"
        />
      </Field>
      <CsvViewer content={text} fileName="countries.csv" height={280} />
    </div>
  )
}

Live page: https://glyphui.xyz/components/csv-viewer