Skip to content

Commit 321f452

Browse files
authored
Cleanup a11y and TS warnings/errors (#207)
* Add workspace level (recursive) `pnpm check` * Fix VSCode not displaying errors due to monorepo handling of mdsvex layout. Related: pngwn/MDsveX#556 * Cleanup warnings/TS errors on docs components (except GeoDebug) * Add `chartContext()` to get strongly typed chart context (from LayerCake) * More TS / a11y improvements * More TS / a11y improvements * Fix/workaround remaining TS / a11y improvements in src/lib * Fix remaining a11y warnings * Add types to topojson data and other type improvements * More progress * Ignore autoType TS error (unsure how to make it happy) * More type progress (AnyScale, etc) * More type progress * More type progress * More progress (90 to go) * Rework accessor and use consistently. More type fixes * Improve types of create series data generators * Fix ts warnings on Bars and Columns examples * Replace `@ts-ignore` with `@ts-expect-error` * More type fixes/workarounds * Update Svelte UX with improved MenuOption type * Hacks to make typescript happy in template * Allow <Chart {xDomain}> to be `null` or `null[]` to support Brush use cases * Support array as well as tuple for `null` | `number` domains * [Brush] Support passing Date for min/max. Cast `Date[]` xDomain example as `[Date, Date]` * Add changesets * Run svelte-check during CI * Add missing `@types/d3-color` and `@types/d3-path` packages * [Chart] Improve default value jsdoc comments * Add changeset * Fix createDimensionGetter() for non-band scales (ex. <Labels> on Area chart) * Use pattern for individual bar highlighting * Tween all histogram examples * Fix sparkbar tooltip example * Hide overflow on ForceGraph examples * Cleanup console.logs
1 parent 5649022 commit 321f452

File tree

182 files changed

+1866
-1059
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+1866
-1059
lines changed

.changeset/early-bugs-grab.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': minor
3+
---
4+
5+
Cleanup a11y and TS warnings/errors

.changeset/light-toys-end.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
Add type-safe `chartContext()` instead of using `getContext('LayerCake')`

.changeset/quick-cooks-marry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': minor
3+
---
4+
5+
Improve types across project

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727

2828
- run: pnpm install --frozen-lockfile
2929

30+
- run: pnpm check
31+
3032
- run: pnpm lint
3133

3234
- run: pnpm test:unit

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"test:unit": "pnpm -r test:unit",
99
"build": "rimraf packages/*/dist && pnpm -r build",
10+
"check": "pnpm -r check",
1011
"lint": "pnpm -r lint",
1112
"format": "pnpm -r format",
1213
"changeset": "changeset",

packages/layerchart/mdsvex.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { fileURLToPath } from 'node:url';
2+
import { dirname } from 'node:path';
13
import slug from 'rehype-slug';
24

35
export default {
46
extensions: ['.md', '.svx'],
5-
layout: './src/lib/docs/Layout.svelte',
7+
// https://github.com/pngwn/MDsveX/issues/556
8+
layout: dirname(fileURLToPath(import.meta.url)) + '/src/lib/docs/Layout.svelte',
69
rehypePlugins: [slug],
710
// highlight: false
811
};

packages/layerchart/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
2929
"@tailwindcss/typography": "^0.5.13",
3030
"@types/d3-array": "^3.2.1",
31+
"@types/d3-color": "^3.1.3",
3132
"@types/d3-delaunay": "^6.0.4",
3233
"@types/d3-dsv": "^3.0.7",
3334
"@types/d3-force": "^3.0.10",
3435
"@types/d3-geo": "^3.1.0",
3536
"@types/d3-hierarchy": "^3.1.7",
3637
"@types/d3-interpolate": "^3.0.4",
3738
"@types/d3-interpolate-path": "^2.0.3",
39+
"@types/d3-path": "^3.1.0",
3840
"@types/d3-quadtree": "^3.0.6",
3941
"@types/d3-random": "^3.0.3",
4042
"@types/d3-sankey": "^0.12.4",
@@ -47,6 +49,7 @@
4749
"@types/shapefile": "^0.6.4",
4850
"@types/topojson-client": "^3.1.4",
4951
"@types/topojson-simplify": "^3.0.3",
52+
"@types/topojson-specification": "^1.0.5",
5053
"autoprefixer": "^10.4.19",
5154
"marked": "^13.0.0",
5255
"mdsvex": "^0.11.2",
@@ -99,7 +102,7 @@
99102
"lodash-es": "^4.17.21",
100103
"posthog-js": "^1.140.1",
101104
"shapefile": "^0.6.6",
102-
"svelte-ux": "^0.71.0",
105+
"svelte-ux": "^0.71.1",
103106
"topojson-client": "^3.1.0"
104107
},
105108
"peerDependencies": {

packages/layerchart/src/app.d.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
declare global {
2-
namespace App {
3-
// interface Error {}
4-
// interface Locals {}
5-
// interface PageData {}
6-
// interface Platform {}
1+
// See https://kit.svelte.dev/docs/types#app
2+
// for information about these interfaces
3+
// and what to do when importing types
4+
declare namespace App {
5+
// interface Error {}
6+
// interface Locals {}
7+
8+
interface PageData {
9+
meta: {
10+
title?: string;
11+
description?: string;
12+
source?: string;
13+
pageSource?: string;
14+
api?: SveldJson;
15+
features?: string[];
16+
related?: string[];
17+
hideUsage?: boolean;
18+
hideTableOfContents?: boolean;
19+
status?: string;
20+
};
721
}
8-
}
922

10-
export {};
23+
// interface PageState {}
24+
// interface Platform {}
25+
}

packages/layerchart/src/lib/components/Arc.svelte

+29-22
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
// https://svelte.dev/repl/09711e43a1264ba18945d7db7cab9335?version=3.38.2
1818
// https://codepen.io/simeydotme/pen/rrOEmO/
1919
20-
import { getContext, tick } from 'svelte';
20+
import { tick } from 'svelte';
2121
import type { SVGAttributes } from 'svelte/elements';
2222
import type { spring as springStore, tweened as tweenedStore } from 'svelte/motion';
2323
import { arc as d3arc } from 'd3-shape';
2424
import { scaleLinear } from 'd3-scale';
2525
import { min, max } from 'd3-array';
2626
27+
import { chartContext } from './ChartContext.svelte';
2728
import { motionStore } from '$lib/stores/motionStore.js';
2829
import { degreesToRadians } from '$lib/utils/math.js';
2930
@@ -80,7 +81,7 @@
8081
8182
export let track: boolean | SVGAttributes<SVGPathElement> = false;
8283
83-
const { yRange } = getContext('LayerCake');
84+
const { yRange } = chartContext();
8485
8586
$: scale = scaleLinear().domain(domain).range(range);
8687
@@ -102,7 +103,7 @@
102103
}
103104
}
104105
105-
$: _outerRadius = getOuterRadius(outerRadius, max($yRange) / 2);
106+
$: _outerRadius = getOuterRadius(outerRadius, (max<number>($yRange) ?? 0) / 2);
106107
107108
function getInnerRadius(innerRadius: number | undefined, outerRadius: number) {
108109
if (innerRadius == null) {
@@ -129,7 +130,7 @@
129130
.startAngle(startAngle ?? degreesToRadians(range[0]))
130131
.endAngle(endAngle ?? degreesToRadians(scale($tweened_value)))
131132
.cornerRadius(cornerRadius)
132-
.padAngle(padAngle);
133+
.padAngle(padAngle) as Function;
133134
// .padRadius(padRadius);
134135
135136
$: trackArc = d3arc()
@@ -138,45 +139,51 @@
138139
.startAngle(startAngle ?? degreesToRadians(range[0]))
139140
.endAngle(endAngle ?? degreesToRadians(range[1]))
140141
.cornerRadius(cornerRadius)
141-
.padAngle(padAngle);
142+
.padAngle(padAngle) as Function;
142143
// .padRadius(padRadius);
143144
145+
// @ts-expect-error
144146
$: trackArcCentroid = trackArc.centroid();
145147
// $: console.log(trackArcCentroid)
146148
147-
let trackArcEl;
148-
$: boundingBox = trackArc && trackArcEl ? trackArcEl.getBBox() : {};
149-
// $: console.log(boundingBox)
149+
let trackArcEl: SVGPathElement | undefined = undefined;
150+
$: boundingBox = trackArcEl ? trackArcEl.getBBox() : ({} as DOMRect);
150151
151-
$: labelArcCenterOffset = {
152-
x: outerRadius - boundingBox.width / 2,
153-
// x: 0,
154-
y: (outerRadius - boundingBox.height / 2) * -1,
155-
};
152+
// $: labelArcCenterOffset = {
153+
// x: outerRadius - boundingBox.width / 2,
154+
// // x: 0,
155+
// y: (outerRadius - boundingBox.height / 2) * -1,
156+
// };
156157
// $: console.log(labelArcCenterOffset)
157158
158-
$: labelArcBottomOffset = {
159-
// x: outerRadius - boundingBox.width / 2,
160-
x: outerRadius - boundingBox.width / 2,
161-
// x: 0,
162-
// y: (outerRadius - boundingBox.height) * -1
163-
y: (outerRadius - boundingBox.height) * -1,
164-
};
159+
// $: labelArcBottomOffset = {
160+
// // x: outerRadius - boundingBox.width / 2,
161+
// x: outerRadius - boundingBox.width / 2,
162+
// // x: 0,
163+
// // y: (outerRadius - boundingBox.height) * -1
164+
// y: (outerRadius - boundingBox.height) * -1,
165+
// };
165166
// $: console.log(labelArcBottomOffset)
166167
167168
/**
168169
* Offset arc from center
169170
*/
170171
export let offset = 0;
171-
$: angle = (startAngle + endAngle) / 2;
172+
$: angle = ((startAngle ?? 0) + (endAngle ?? 0)) / 2;
172173
$: xOffset = Math.sin(angle) * offset;
173174
$: yOffset = -Math.cos(angle) * offset;
174175
</script>
175176

176177
{#if track}
177-
<path d={trackArc()} class="track" bind:this={trackArcEl} {...track} />
178+
<path
179+
d={trackArc()}
180+
class="track"
181+
bind:this={trackArcEl}
182+
{...typeof track === 'object' ? track : null}
183+
/>
178184
{/if}
179185

186+
<!-- svelte-ignore a11y-no-static-element-interactions -->
180187
<path
181188
d={arc()}
182189
transform="translate({xOffset}, {yOffset})"

packages/layerchart/src/lib/components/Area.svelte

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { getContext, type ComponentProps } from 'svelte';
2+
import { type ComponentProps } from 'svelte';
33
import type { tweened as tweenedStore } from 'svelte/motion';
44
import { type Area, area as d3Area, areaRadial } from 'd3-shape';
55
import type { CurveFactory } from 'd3-shape';
@@ -10,9 +10,10 @@
1010
1111
import { motionStore } from '$lib/stores/motionStore.js';
1212
13+
import { chartContext } from './ChartContext.svelte';
1314
import Spline from './Spline.svelte';
1415
15-
const { data: contextData, xScale, yScale, xGet, yGet, yRange } = getContext('LayerCake');
16+
const { data: contextData, xScale, yScale, xGet, yGet, yRange } = chartContext();
1617
1718
/** Override data instead of using context */
1819
export let data: any = undefined;
@@ -24,12 +25,12 @@
2425
export let radial = false;
2526
2627
/** Override x accessor */
27-
export let x: any = undefined; // TODO: Update Type
28+
export let x: ((d: any) => any) | undefined = undefined;
2829
2930
/** Override y0 accessor. Defaults to max($yRange) */
30-
export let y0: any = undefined; // TODO: Update Type
31+
export let y0: ((d: any) => any) | undefined = undefined;
3132
/** Override y1 accessor. Defaults to y accessor */
32-
export let y1: any = undefined; // TODO: Update Type
33+
export let y1: ((d: any) => any) | undefined = undefined;
3334
3435
/** Interpolate path data using d3-interpolate-path */
3536
export let tweened: boolean | Parameters<typeof tweenedStore>[1] = undefined;
@@ -42,7 +43,9 @@
4243
/** Enable showing line */
4344
export let line: boolean | Partial<ComponentProps<Spline>> = false;
4445
45-
$: tweenedOptions = tweened ? { interpolate: interpolatePath, ...tweened } : false;
46+
$: tweenedOptions = tweened
47+
? { interpolate: interpolatePath, ...(typeof tweened === 'object' ? tweened : null) }
48+
: false;
4649
$: tweened_d = motionStore('', { tweened: tweenedOptions });
4750
$: {
4851
const path = radial
@@ -58,14 +61,15 @@
5861
if (defined) path.defined(defined);
5962
6063
const d = pathData ?? path(data ?? $contextData);
61-
tweened_d.set(d);
64+
tweened_d.set(d ?? '');
6265
}
6366
</script>
6467

6568
{#if line}
6669
<Spline {data} y={y1} {curve} {defined} {tweened} {...typeof line === 'object' ? line : null} />
6770
{/if}
6871

72+
<!-- svelte-ignore a11y-no-static-element-interactions -->
6973
<path
7074
d={$tweened_d}
7175
clip-path={clipPath}

packages/layerchart/src/lib/components/AreaStack.svelte

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<script lang="ts">
2-
import { getContext, type ComponentProps } from 'svelte';
2+
import { type ComponentProps } from 'svelte';
33
import type { tweened as tweenedStore } from 'svelte/motion';
44
import type { CurveFactory } from 'd3-shape';
55
6+
import { chartContext } from './ChartContext.svelte';
67
import Area from './Area.svelte';
78
import Spline from './Spline.svelte';
89
9-
const { data, rGet } = getContext('LayerCake');
10-
11-
// TODO: Add as generic
12-
type Datum = any;
10+
const { data, rGet } = chartContext();
1311
1412
export let curve: CurveFactory | undefined = undefined;
1513
export let defined: ComponentProps<Spline>['defined'] | undefined = undefined;

0 commit comments

Comments
 (0)