Skip to content

Feat/reuse link wrapper #109

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 7 commits into from
Jun 2, 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
44 changes: 18 additions & 26 deletions src/lib/marks/Area.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import { maybeCurve } from '$lib/helpers/curves.js';
import { isValid } from '$lib/helpers/index.js';
import AreaCanvas from './helpers/AreaCanvas.svelte';
import Anchor from './helpers/Anchor.svelte';

import type {
CurveName,
Expand All @@ -37,7 +38,6 @@
BaseMarkProps,
ConstantAccessor,
ChannelAccessor,
FacetContext,
ScaledDataRecord,
LinkableMarkProps
} from '../types.js';
Expand Down Expand Up @@ -113,32 +113,24 @@
{:else}
<GroupMultiple length={grouped.length}>
{#each grouped as areaData, i (i)}
{#snippet el(datum: ScaledDataRecord)}
{@const title = resolveProp(options.title, datum.datum, '')}
{@const [style, styleClass] = resolveStyles(
plot,
datum,
options,
'fill',
usedScales
)}
<path
class={['svelteplot-area', className, styleClass]}
clip-path={options.clipPath}
d={areaPath(areaData)}
{style}
>{#if title}<title>{title}</title>{/if}</path>
{/snippet}
{@const datum = areaData[0]}
{#if areaData.length > 0}
{#if options.href}
<a
href={resolveProp(options.href, areaData[0].datum, '')}
target={resolveProp(options.target, areaData[0].datum, '_self')}>
{@render el(areaData[0])}
</a>
{:else}
{@render el(areaData[0])}
{/if}
<Anchor {options} {datum}>
{@const title = resolveProp(options.title, datum.datum, '')}
{@const [style, styleClass] = resolveStyles(
plot,
datum,
options,
'fill',
usedScales
)}
<path
class={['svelteplot-area', className, styleClass]}
clip-path={options.clipPath}
d={areaPath(areaData)}
{style}
>{#if title}<title>{title}</title>{/if}</path>
</Anchor>
{/if}
{/each}
</GroupMultiple>
Expand Down
4 changes: 3 additions & 1 deletion src/lib/marks/BarX.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
PlotContext,
BaseMarkProps,
BaseRectMarkProps,
ChannelAccessor
ChannelAccessor,
LinkableMarkProps
} from '../types.js';

export type BarXMarkProps = BaseMarkProps &
LinkableMarkProps &
BaseRectMarkProps & {
data: DataRow[];
x?: ChannelAccessor;
Expand Down
1 change: 1 addition & 0 deletions src/lib/marks/BarY.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
} from '../types.js';

export type BarYMarkProps = BaseMarkProps &
LinkableMarkProps &
BaseRectMarkProps & {
data: DataRow[];
x?: ChannelAccessor;
Expand Down
1 change: 1 addition & 0 deletions src/lib/marks/Cell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
} from '../types.js';

export type CellMarkProps = BaseMarkProps &
LinkableMarkProps &
BaseRectMarkProps & {
data: DataRecord[];
x?: ChannelAccessor;
Expand Down
35 changes: 19 additions & 16 deletions src/lib/marks/Dot.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
symbol?: ChannelAccessor | Snippet<[number, string]>;
canvas?: boolean;
dotClass?: ConstantAccessor<string>;
};
} & LinkableMarkProps;
</script>

<script lang="ts">
Expand All @@ -21,8 +21,8 @@
BaseMarkProps,
ConstantAccessor,
ChannelAccessor,
FacetContext,
PlotDefaults
PlotDefaults,
LinkableMarkProps
} from '../types.js';
import { resolveProp, resolveStyles } from '../helpers/resolve.js';
import { maybeSymbol } from '$lib/helpers/symbols.js';
Expand All @@ -33,6 +33,7 @@
import { maybeData, isValid } from '$lib/helpers/index.js';
import { recordizeXY } from '$lib/transforms/recordize.js';
import { addEventHandlers } from './helpers/events.js';
import Anchor from './helpers/Anchor.svelte';

let {
data = [{}],
Expand Down Expand Up @@ -95,19 +96,21 @@
'stroke',
usedScales
)}
<path
transform="translate({d.x}, {d.y})"
d={getSymbolPath(d.symbol, d.r ** 2 * Math.PI)}
class={[
dotClass ? resolveProp(dotClass, d.datum, null) : null,
styleClass
]}
{style}
use:addEventHandlers={{
getPlotState,
options: args,
datum: d.datum
}} />
<Anchor {options} datum={d.datum}>
<path
transform="translate({d.x}, {d.y})"
d={getSymbolPath(d.symbol, d.r ** 2 * Math.PI)}
class={[
dotClass ? resolveProp(dotClass, d.datum, null) : null,
styleClass
]}
{style}
use:addEventHandlers={{
getPlotState,
options: args,
datum: d.datum
}} />
</Anchor>
{/if}
{/each}
{/if}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/marks/Frame.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
BaseMarkProps,
'fill' | 'stroke' | 'fillOpacity' | 'strokeOpacity'
> &
LinkableMarkProps &
Omit<
BaseRectMarkProps,
'inset' | 'insetLeft' | 'insetRight' | 'insetTop' | 'insetBottom'
Expand All @@ -27,7 +28,7 @@
<script lang="ts">
import Mark from '../Mark.svelte';
import { getContext } from 'svelte';
import type { PlotContext, BaseRectMarkProps } from '../types.js';
import type { PlotContext, BaseRectMarkProps, LinkableMarkProps } from '../types.js';
import type { BaseMarkProps } from '../types.js';
import RectPath from './helpers/RectPath.svelte';

Expand Down
60 changes: 27 additions & 33 deletions src/lib/marks/Geo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
geoType?: 'sphere' | 'graticule';
dragRotate: boolean;
canvas: boolean;
href: ConstantAccessor<string>;
target: ConstantAccessor<string>;
/**
* simple browser tooltip to be displayed on mouseover
*/
title: ConstantAccessor<string>;
} & BaseMarkProps &
LinkableMarkProps;
</script>
Expand All @@ -31,6 +33,7 @@
import GeoCanvas from './helpers/GeoCanvas.svelte';
import { recordize } from '$lib/transforms/recordize.js';
import { GEOJSON_PREFER_STROKE } from '$lib/helpers/index.js';
import Anchor from './helpers/Anchor.svelte';

const { getPlotState } = getContext<PlotContext>('svelteplot');
const plot = $derived(getPlotState());
Expand Down Expand Up @@ -68,28 +71,6 @@
channels={['fill', 'stroke', 'opacity', 'fillOpacity', 'strokeOpacity', 'r']}
{...args}>
{#snippet children({ mark, scaledData, usedScales })}
{#snippet el(d)}
{@const title = resolveProp(args.title, d.datum, '')}
{@const geometry = resolveProp(args.geometry, d.datum, d.datum)}
{@const [style, styleClass] = resolveStyles(
plot,
d,
args,
GEOJSON_PREFER_STROKE.has(geometry.type) ? 'stroke' : 'fill',
usedScales
)}
<path
d={path(geometry)}
{style}
class={[styleClass]}
use:addEventHandlers={{
getPlotState,
options: args,
datum: d.datum
}}>
{#if title}<title>{title}</title>{/if}
</path>
{/snippet}
<g
aria-label="geo"
class={['geo', geoType && `geo-${geoType}`, className]}
Expand All @@ -99,15 +80,28 @@
{:else}
{#each scaledData as d, i (i)}
{#if d.valid}
{#if options.href}
<a
href={resolveProp(args.href, d.datum, '')}
target={resolveProp(args.target, d.datum, '_self')}>
{@render el(d)}
</a>
{:else}
{@render el(d)}
{/if}
<Anchor {options} datum={d.datum}>
{@const title = resolveProp(args.title, d.datum, '')}
{@const geometry = resolveProp(args.geometry, d.datum, d.datum)}
{@const [style, styleClass] = resolveStyles(
plot,
d,
args,
GEOJSON_PREFER_STROKE.has(geometry.type) ? 'stroke' : 'fill',
usedScales
)}
<path
d={path(geometry)}
{style}
class={[styleClass]}
use:addEventHandlers={{
getPlotState,
options: args,
datum: d.datum
}}>
{#if title}<title>{title}</title>{/if}
</path>
</Anchor>
{/if}
{/each}
{/if}
Expand Down
1 change: 1 addition & 0 deletions src/lib/marks/Rect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
y2?: ChannelAccessor;
interval?: number | string;
} & BaseMarkProps &
LinkableMarkProps &
BaseRectMarkProps;
</script>

Expand Down
4 changes: 2 additions & 2 deletions src/lib/marks/Sphere.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Geo mark with Sphere geometry object -->

<script module lang="ts">
import { type BaseMarkProps } from '$lib/types.js';
export type SphereMarkProps = BaseMarkProps;
import { type BaseMarkProps, type LinkableMarkProps } from '$lib/types.js';
export type SphereMarkProps = BaseMarkProps & LinkableMarkProps;
</script>

<script lang="ts">
Expand Down
37 changes: 37 additions & 0 deletions src/lib/marks/helpers/Anchor.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script>
import { resolveProp } from '$lib/helpers/resolve';

let { datum = {}, options = {}, children } = $props();

const href = $derived(resolveProp(options.href, datum, null));
const target = $derived(resolveProp(options.target, datum, null));
const rel = $derived(resolveProp(options.rel, datum, null));
const type = $derived(resolveProp(options.type, datum, null));
const download = $derived(resolveProp(options.download, datum, null));

// filter data attributes from options
const dataAttributes = $derived(
Object.fromEntries(
Object.entries(options).filter(([key]) => key.startsWith('data-sveltekit-'))
)
);
</script>

{#if href}
<!-- we can't use <a> directly here because Svelte confuses it with the
HTMLAElement which breaks the rendering -->
<svelte:element
this={'a'}
{href}
{target}
{rel}
{type}
{download}
{...dataAttributes}
aria-label="link"
xmlns="http://www.w3.org/2000/svg">
{@render children?.()}
</svelte:element>
{:else}
{@render children?.()}
{/if}
63 changes: 33 additions & 30 deletions src/lib/marks/helpers/RectPath.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Helper component for rendering rectangular marks in SVG
} from 'svelteplot/types';
import { addEventHandlers } from './events';
import { getContext } from 'svelte';
import Anchor from './Anchor.svelte';

let {
datum,
Expand Down Expand Up @@ -101,33 +102,35 @@ Helper component for rendering rectangular marks in SVG
);
</script>

{#if hasBorderRadius}
<path
transform="translate({x + dx + insetLeft},{y + insetBottom + dy})"
d={roundedRect(
0,
0,
width - insetLeft - insetRight,
height - insetTop - insetBottom,
borderRadius
)}
class={[styleClass, className]}
{style}
use:addEventHandlers={{
getPlotState,
options,
datum: datum?.datum
}} />
{:else}
<rect
transform="translate({x + dx + insetLeft},{y + insetBottom + dy})"
width={width - insetLeft - insetRight}
height={height - insetTop - insetBottom}
class={[styleClass, className]}
{style}
use:addEventHandlers={{
getPlotState,
options,
datum: datum?.datum
}} />
{/if}
<Anchor {options} datum={datum?.datum}>
{#if hasBorderRadius}
<path
transform="translate({x + dx + insetLeft},{y + insetBottom + dy})"
d={roundedRect(
0,
0,
width - insetLeft - insetRight,
height - insetTop - insetBottom,
borderRadius
)}
class={[styleClass, className]}
{style}
use:addEventHandlers={{
getPlotState,
options,
datum: datum?.datum
}} />
{:else}
<rect
transform="translate({x + dx + insetLeft},{y + insetBottom + dy})"
width={width - insetLeft - insetRight}
height={height - insetTop - insetBottom}
class={[styleClass, className]}
{style}
use:addEventHandlers={{
getPlotState,
options,
datum: datum?.datum
}} />
{/if}
</Anchor>
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