Skip to content

feat: add text.lineHeight + support css variable font sizes #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ export default [
files: ['**/*.svelte', '*.svelte'],
rules: {
'svelte/no-object-in-text-mustaches': 'warn',
'svelte/no-inspect': 'warn'
'svelte/no-inspect': 'warn',
'svelte/no-useless-mustaches': [
'error',
{
ignoreStringEscape: true
}
]
},
ignores: ['dist/*', '.sveltepress/*'],
languageOptions: {
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@sveltejs/adapter-auto": "^6.0.1",
"@sveltejs/adapter-static": "^3.0.8",
"@sveltejs/eslint-config": "^8.2.0",
"@sveltejs/kit": "^2.21.2",
"@sveltejs/kit": "^2.21.4",
"@sveltejs/package": "^2.3.11",
"@sveltejs/vite-plugin-svelte": "5.1.0",
"@sveltepress/theme-default": "^6.0.3",
Expand All @@ -72,35 +72,35 @@
"@types/d3-scale": "^4.0.9",
"@types/d3-scale-chromatic": "^3.1.0",
"@types/d3-shape": "^3.1.7",
"@typescript-eslint/eslint-plugin": "^8.33.1",
"@typescript-eslint/parser": "^8.33.1",
"@typescript-eslint/eslint-plugin": "^8.34.0",
"@typescript-eslint/parser": "^8.34.0",
"csstype": "^3.1.3",
"d3-dsv": "^3.0.1",
"d3-fetch": "^3.0.1",
"d3-force": "^3.0.0",
"eslint": "^9.28.0",
"eslint-config-prettier": "^10.1.5",
"eslint-plugin-svelte": "3.9.1",
"eslint-plugin-svelte": "3.9.2",
"jsdom": "^26.1.0",
"prettier": "^3.5.3",
"prettier-plugin-svelte": "^3.4.0",
"puppeteer": "^24.10.0",
"remark-code-extra": "^1.0.1",
"remark-code-frontmatter": "^1.0.0",
"resize-observer-polyfill": "^1.5.1",
"sass": "^1.89.1",
"sass": "^1.89.2",
"svelte-check": "^4.2.1",
"svelte-eslint-parser": "1.2.0",
"svelte-highlight": "^7.8.3",
"svg-path-parser": "^1.1.0",
"topojson-client": "^3.1.0",
"ts-essentials": "^10.0.4",
"ts-essentials": "^10.1.0",
"tslib": "^2.8.1",
"typedoc": "^0.28.5",
"typedoc-plugin-markdown": "^4.6.4",
"typescript": "^5.8.3",
"vite": "^6.3.5",
"vitest": "^3.2.2",
"vitest": "^3.2.3",
"vitest-matchmedia-mock": "^2.0.3"
},
"types": "./dist/index.d.ts",
Expand All @@ -119,9 +119,9 @@
"d3-scale-chromatic": "^3.1.0",
"d3-shape": "^3.2.0",
"d3-time": "^3.1.0",
"es-toolkit": "^1.39.1",
"es-toolkit": "^1.39.3",
"fast-equals": "^5.2.2",
"merge-deep": "^3.0.3",
"svelte": "5.33.14"
"svelte": "5.33.19"
}
}
472 changes: 243 additions & 229 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/lib/helpers/getBaseStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ export function maybeToPixel(cssKey: string, value: string | number) {
}
return value;
}

export function maybeFromPixel(value: string | number) {
return typeof value === 'string' && value.endsWith('px') ? +value.slice(0, -2) : value;
}

export function maybeFromRem(value: string | number, rootFontSize: number = 16) {
return typeof value === 'string' && value.endsWith('rem')
? +value.slice(0, -3) * rootFontSize
: value;
}
1 change: 1 addition & 0 deletions src/lib/helpers/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function resolveProp<T>(
) {
return datum[accessor] as T;
}

return isRawValue(accessor) ? accessor : _defaultValue;
}

Expand Down
112 changes: 10 additions & 102 deletions src/lib/marks/Text.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
* the line anchor for vertical position; top, bottom, or middle
*/
lineAnchor?: ConstantAccessor<'bottom' | 'top' | 'middle'>;
/**
* line height as multiplier of font size
* @default 1.2
*/
lineHeight?: ConstantAccessor<number>;
frameAnchor?: ConstantAccessor<
| 'bottom'
| 'top'
Expand All @@ -35,7 +40,6 @@
import { getContext, type Snippet } from 'svelte';
import GroupMultiple from './helpers/GroupMultiple.svelte';
import type {
PlotContext,
DataRecord,
BaseMarkProps,
ConstantAccessor,
Expand All @@ -46,11 +50,14 @@
import Mark from '../Mark.svelte';
import { sort } from '$lib/index.js';

import MultilineText from './helpers/MultilineText.svelte';

const DEFAULTS = {
fontSize: 12,
fontWeight: 500,
strokeWidth: 1.6,
frameAnchor: 'center',
lineHeight: 1.1,
...getContext<PlotDefaults>('svelteplot/_defaults').text
};

Expand All @@ -65,21 +72,12 @@
...markProps
});

const { getPlotState } = getContext<PlotContext>('svelteplot');
let plot = $derived(getPlotState());

const LINE_ANCHOR = {
bottom: 'auto',
middle: 'central',
top: 'hanging'
} as const;

const args = $derived(
sort({
data,
...options
})
);
) as TextMarkProps;
</script>

<Mark
Expand All @@ -101,99 +99,9 @@
<GroupMultiple class="text {className}" length={className ? 2 : args.data.length}>
{#each scaledData as d, i (i)}
{#if d.valid}
{@const title = resolveProp(args.title, d.datum, '')}
{@const frameAnchor = resolveProp(args.frameAnchor, d.datum)}
{@const isLeft =
frameAnchor === 'left' ||
frameAnchor === 'top-left' ||
frameAnchor === 'bottom-left'}
{@const isRight =
frameAnchor === 'right' ||
frameAnchor === 'top-right' ||
frameAnchor === 'bottom-right'}
{@const isTop =
frameAnchor === 'top' ||
frameAnchor === 'top-left' ||
frameAnchor === 'top-right'}
{@const isBottom =
frameAnchor === 'bottom' ||
frameAnchor === 'bottom-left' ||
frameAnchor === 'bottom-right'}
{@const [x, y] =
args.x != null && args.y != null
? [d.x, d.y]
: [
args.x != null
? d.x
: isLeft
? plot.options.marginLeft
: isRight
? plot.options.marginLeft + plot.facetWidth
: plot.options.marginLeft + plot.facetWidth * 0.5,
args.y != null
? d.y
: isTop
? plot.options.marginTop
: isBottom
? plot.options.marginTop + plot.facetHeight
: plot.options.marginTop + plot.facetHeight * 0.5
]}

{@const dx = +resolveProp(args.dx, d.datum, 0)}
{@const dy = +resolveProp(args.dy, d.datum, 0)}
{@const textLines = String(resolveProp(args.text, d.datum, '')).split('\n')}
{@const lineAnchor = resolveProp(
args.lineAnchor,
d.datum,
args.y != null ? 'middle' : isTop ? 'top' : isBottom ? 'bottom' : 'middle'
)}
{@const textClassName = resolveProp(args.textClass, d.datum, null)}

{@const [style, styleClass] = resolveStyles(
plot,
{ ...d, __tspanIndex: 0 },
{
fontSize: 12,
fontWeight: 500,
strokeWidth: 1.6,
textAnchor: isLeft ? 'start' : isRight ? 'end' : 'middle',
...args
},
'fill',
usedScales
)}

{#if textLines.length > 1}
<!-- multiline text-->
{@const fontSize = resolveProp(args.fontSize, d.datum) || 12}
<text
class={[textClassName]}
dominant-baseline={LINE_ANCHOR[lineAnchor]}
transform="translate({Math.round(x + dx)},{Math.round(
y +
dy -
(lineAnchor === 'bottom'
? textLines.length - 1
: lineAnchor === 'middle'
? (textLines.length - 1) * 0.5
: 0) *
fontSize
)})"
>{#each textLines as line, l (l)}<tspan
x="0"
dy={l ? fontSize : 0}
class={styleClass}
{style}>{line}</tspan
>{/each}{#if title}<title>{title}</title>{/if}</text>
{:else}
<!-- singleline text-->
<text
class={[textClassName, styleClass]}
dominant-baseline={LINE_ANCHOR[lineAnchor]}
transform="translate({Math.round(x + dx)},{Math.round(y + dy)})"
{style}
>{textLines[0]}{#if title}<title>{title}</title>{/if}</text>
{/if}
<MultilineText {textLines} {d} {args} {usedScales} />
{/if}
{/each}
</GroupMultiple>
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy