|
1 | 1 | --- |
2 | | - title: Test Of mdsvex-table |
| 2 | + title: mdsvex-table |
| 3 | + description: | |
| 4 | + Full documentation for mdsvex-table, an NPM package for Svelte web applications that renders a responsive table out of tables generated with the popular mdsvex package and Markdown documents. |
3 | 5 | --- |
4 | | -# {title} |
5 | 6 |
|
6 | 7 | <script> |
7 | 8 | import { CirclePlus } from '@lucide/svelte'; |
8 | 9 | </script> |
9 | 10 |
|
10 | | -| Item | <CirclePlus size="1.1em" /> Description | |
11 | | -| - | - | |
12 | | -| Alpha 1 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reiciendis eius assumenda in deleniti recusandae fugiat suscipit iusto ipsam vitae consequuntur quas ex, incidunt, amet corrupti quam veritatis doloremque itaque nostrum cum. Repudiandae, fugit consequatur? Qui laborum ea expedita debitis mollitia repellat ex nam ab earum facere veritatis delectus, voluptate praesentium?| |
13 | | -| Beta 2 | Lorem ipsum dolor sit amet consectetur adipisicing elit. Nemo, ipsum ad voluptate exercitationem corrupti voluptates fugiat qui culpa vero totam? | |
14 | | -| Gamma 3 | [CollageJS](https://collagejs.dev) | |
| 11 | +# {title} |
| 12 | + |
| 13 | +This is a Svelte v5 NPM package that provides replacement Svelte components for the necessary HTML elements that compose a table: `table`, `thead`, `tbody`, `tr`, `th` and `td`. These are the ones that can be generated out of a Markdown table. Replacements for `caption` and `tfooter` are not provided. The components are designed to exchange the table markup with list markup (not `ul/ol`, but a list of containers) that can be read better in small devices. In other words: Transforms table to a vertical list of card-like items. |
| 14 | + |
| 15 | +## How It Works |
| 16 | + |
| 17 | +Since this is a component meant to replace Markdown-generated tables, it doesn't rely on component properties. Instead, configuration and all interconnection between the various table components is done through context. |
| 18 | + |
| 19 | +We also don't use the components directly under normal circumstances. Instead, we configure a layout component in *MDsveX* and follow their instructions on how to export all the table-related components. |
| 20 | + |
| 21 | +We finally initialize the whole thing by creating the "root" table context that will apply to all tables in the Markdown document. |
| 22 | + |
| 23 | +These would be the additions to the layout component: |
| 24 | + |
| 25 | +```svelte |
| 26 | +<script lang="ts" module> |
| 27 | + // export * ... doesn't work. 😢 |
| 28 | + export { |
| 29 | + table, |
| 30 | + thead, |
| 31 | + tbody, |
| 32 | + tr, |
| 33 | + th, |
| 34 | + td |
| 35 | + } from 'mdsvex-table'; |
| 36 | +</script> |
| 37 | +<script lang="ts"> |
| 38 | + import { setTableContext, TableContext } from 'mdsvex-table'; |
| 39 | +
|
| 40 | + setTableContext(new TableContext({ /* options */ })); |
| 41 | +</script> |
| 42 | +``` |
| 43 | + |
| 44 | +## Options |
| 45 | + |
| 46 | +| Option | Type | Default | Description | |
| 47 | +| - | - | - | - | |
| 48 | +| `breakpoint` | `string \| number \| (() => string)` | `576` | The breakpoint at which the list format kicks in. If it is a number, it is assumed to be in pixels; if it is a string (such as `'25rem'`) then it is given as-is to the media query; if it is a function, it must return the entire media query. | |
| 49 | +| `ssrBehavior` | `"list" \| "table"` | `'table'` | For Sveltekit and SSR-rendered implementations, it determines how the table will be rendered. |
| 50 | + |
| 51 | +## Styling |
| 52 | + |
| 53 | +For the table, continue styling it the way you've been doing it. |
| 54 | + |
| 55 | +For the list version, this is how it can be styled: |
| 56 | + |
| 57 | +```css |
| 58 | +[data-mdsvex-table="table"] { |
| 59 | + /* styles */ |
| 60 | +} |
| 61 | + |
| 62 | +[data-mdsvex-table="tbody"] { |
| 63 | + /* styles */ |
| 64 | +} |
| 65 | + |
| 66 | +[data-mdsvex-table="tr"] { |
| 67 | + /* styles */ |
| 68 | +} |
| 69 | + |
| 70 | +[data-mdsvex-table="th"] { |
| 71 | + /* styles */ |
| 72 | +} |
| 73 | + |
| 74 | +[data-mdsvex-table="td"] { |
| 75 | + /* styles */ |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Yes, there's also an element that can be targeted with `[data-mdsvex-table="thead"]`, but said element is invisible. It exists only for screen readers, so don't mess with it. |
| 80 | + |
| 81 | +## Development Tips |
| 82 | + |
| 83 | +To improve SSR guessing, 2 easy methods come to mind: |
| 84 | + |
| 85 | +1. Examine the *User Agent* string and determine if the requestor is using a mobile device. |
| 86 | +2. Save and send a cookie with the user's `window.innerWidth` value. Then SSR can be 100% accurate after one visit to your web application. |
| 87 | + |
| 88 | +For #1 above, ask an AI and you shall receive recommendations: |
| 89 | + |
| 90 | +| Package | Features | Use Case | |
| 91 | +| --- | --- | --- | |
| 92 | +| **ua-parser-js** | Widely used, lightweight (~17KB minified). Parses browser, OS, device, CPU, and engine. Works in Node.js when you pass ``req.headers['user-agent']``. | General-purpose parsing with broad ecosystem support. | |
| 93 | +| **ua-parser-modern** | TypeScript-native, modular functions (``parseBrowser``, ``parseOS``, etc.). Compatible with modern bundlers (Vite, Rollup, esbuild). Handles tricky cases like Brave spoofing Chrome. | Ideal for TypeScript projects or modern toolchains. | |
| 94 | +| **browser-dtector** | Simple API, supports both Node.js and browser. Returns parsed info including platform, version, and flags (``isMobile``, ``isDesktop``, etc.). | Quick detection with minimal setup, good for middleware. | |
| 95 | + |
| 96 | +**IMPORTANT:** I did not sanitize the recommendations, so do that yourself and [tell us about it](https://github.com/WJSoftware/mdsvex-table/discussions) if you like. |
| 97 | + |
| 98 | +As for #2, remember to use [`<svelte:window>`](https://svelte.dev/docs/svelte/svelte-window) to keep the cookie reactively up-to-date. |
0 commit comments