Skip to content

Commit 9accd0b

Browse files
committed
fix: use separate svg component and fix all type errors
1 parent 61b9395 commit 9accd0b

32 files changed

+238
-83
lines changed

packages/button/src/Button.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
dispatch,
6363
} from '@smui/common/internal';
6464
import Ripple from '@smui/ripple';
65-
import type { SmuiElementMap, SmuiAttrs } from '@smui/common';
65+
import type { SmuiElementMap, SmuiAttrs, SmuiSvgAttrs } from '@smui/common';
6666
import { SmuiElement } from '@smui/common';
6767
6868
type TagName = $$Generic<keyof SmuiElementMap>;
@@ -82,7 +82,8 @@
8282
component?: Component;
8383
tag?: TagName;
8484
};
85-
type $$Props = OwnProps & SmuiAttrs<keyof SmuiElementMap, OwnProps>;
85+
type $$Props = OwnProps &
86+
(SmuiAttrs<keyof SmuiElementMap, OwnProps> | SmuiSvgAttrs<OwnProps>);
8687
8788
const forwardEvents = forwardEventsBuilder(get_current_component());
8889

packages/chips/src/Chip.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
dispatch,
7070
} from '@smui/common/internal';
7171
import Ripple from '@smui/ripple';
72-
import type { SmuiElementMap, SmuiAttrs } from '@smui/common';
72+
import type { SmuiElementMap, SmuiAttrs, SmuiSvgAttrs } from '@smui/common';
7373
import { SmuiElement } from '@smui/common';
7474
7575
import type { SMUIChipsPrimaryActionAccessor } from './Text.types.js';
@@ -92,7 +92,8 @@
9292
component?: Component;
9393
tag?: TagName;
9494
};
95-
type $$Props = OwnProps & SmuiAttrs<keyof SmuiElementMap, OwnProps>;
95+
type $$Props = OwnProps &
96+
(SmuiAttrs<keyof SmuiElementMap, OwnProps> | SmuiSvgAttrs<OwnProps>);
9697
9798
const forwardEvents = forwardEventsBuilder(get_current_component());
9899

packages/common/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@ The common icon is used everywhere that exports an `Icon` component except for `
3939
- `class`: `''` - A CSS class string.
4040
- `on`: `false` - Used in the context of an icon button toggle to denote the icon for when the button is on.
4141

42-
## Element
42+
## SmuiElement
4343

44-
A dynamic element component.
44+
A dynamic HTML element component.
4545

4646
### Props / Defaults
4747

4848
- `use`: `[]` - An array of Svelte actions and/or arrays of an action and its options.
4949
- `tag`: `'div'` - An HTML tag name to use as the element.
5050

51+
## Svg
52+
53+
An SVG tag component. This is separated from the `SmuiElement` component, because it returns a `SVGSVGElement` object, which does not implement the `HTMLElement` interface.
54+
55+
### Props / Defaults
56+
57+
- `use`: `[]` - An array of Svelte actions and/or arrays of an action and its options.
58+
5159
# Helper Utilities
5260

5361
Helper utilities are exported from the `@smui/common/internal` endpoint. They are used within SMUI to provide additional functionality outside of the features the Svelte API is natively capable of. You can use them in your own components to provide the same additional functionality.

packages/common/src/CommonIcon.svelte

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'mdc-segmented-button__icon': context === 'segmented-button',
1515
})}
1616
aria-hidden="true"
17-
{...tag === 'svg' ? { focusable: 'false', tabindex: '-1' } : {}}
17+
{...svg ? { focusable: 'false', tabindex: '-1' } : {}}
1818
{...$$restProps}><slot /></svelte:component
1919
>
2020

@@ -25,8 +25,12 @@
2525
2626
import type { ActionArray } from './internal/useActions.js';
2727
import { forwardEventsBuilder, classMap } from './internal/index.js';
28-
import type { SmuiElementMap, SmuiAttrs } from './smui.types.js';
29-
import { SmuiElement } from './index.js';
28+
import type {
29+
SmuiElementMap,
30+
SmuiAttrs,
31+
SmuiSvgAttrs,
32+
} from './smui.types.js';
33+
import { SmuiElement, Svg } from './index.js';
3034
3135
type TagName = $$Generic<keyof SmuiElementMap>;
3236
type Component = $$Generic<ComponentType<SvelteComponent>>;
@@ -37,7 +41,8 @@
3741
component?: Component;
3842
tag?: TagName;
3943
};
40-
type $$Props = OwnProps & SmuiAttrs<keyof SmuiElementMap, OwnProps>;
44+
type $$Props = OwnProps &
45+
(SmuiAttrs<keyof SmuiElementMap, OwnProps> | SmuiSvgAttrs<OwnProps>);
4146
4247
const forwardEvents = forwardEventsBuilder(get_current_component());
4348
@@ -54,6 +59,7 @@
5459
component === (SmuiElement as unknown as Component) ? 'svg' : undefined
5560
) as TagName | undefined;
5661
62+
const svg = component === (Svg as unknown as Component);
5763
const context = getContext<string | undefined>('SMUI:icon:context');
5864
5965
export function getElement(): HTMLElement {

packages/common/src/CommonLabel.svelte

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
3030
import type { ActionArray } from './internal/useActions.js';
3131
import { forwardEventsBuilder, classMap } from './internal/index.js';
32-
import type { SmuiElementMap, SmuiAttrs } from './smui.types.js';
32+
import type {
33+
SmuiElementMap,
34+
SmuiAttrs,
35+
SmuiSvgAttrs,
36+
} from './smui.types.js';
3337
import { SmuiElement } from './index.js';
3438
3539
type TagName = $$Generic<keyof SmuiElementMap>;
@@ -40,7 +44,8 @@
4044
component?: Component;
4145
tag?: TagName;
4246
};
43-
type $$Props = OwnProps & SmuiAttrs<keyof SmuiElementMap, OwnProps>;
47+
type $$Props = OwnProps &
48+
(SmuiAttrs<keyof SmuiElementMap, OwnProps> | SmuiSvgAttrs<OwnProps>);
4449
4550
const forwardEvents = forwardEventsBuilder(get_current_component());
4651

packages/common/src/Svg.svelte

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<svg bind:this={element} use:useActions={use} use:forwardEvents {...$$restProps}
2+
><slot /></svg
3+
>
4+
5+
<script lang="ts">
6+
import { get_current_component } from 'svelte/internal';
7+
8+
import type { ActionArray } from './internal/useActions.js';
9+
import { forwardEventsBuilder, useActions } from './internal/index.js';
10+
import type { SmuiSvgAttrs } from './smui.types.js';
11+
12+
type OwnProps = {
13+
use?: ActionArray;
14+
};
15+
type $$Props = OwnProps & SmuiSvgAttrs<OwnProps>;
16+
17+
// Remember to update $$Props if you add/remove/rename props.
18+
export let use: ActionArray = [];
19+
20+
const forwardEvents = forwardEventsBuilder(get_current_component());
21+
22+
let element: SVGSVGElement;
23+
24+
export function getElement() {
25+
return element;
26+
}
27+
</script>

packages/common/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import Label from './CommonLabel.svelte';
33
import Icon from './CommonIcon.svelte';
44

55
import SmuiElement from './SmuiElement.svelte';
6+
import Svg from './Svg.svelte';
67

78
import ContextFragment from './ContextFragment.svelte';
89

9-
export { Label, Icon, SmuiElement, ContextFragment };
10+
export { Label, Icon, SmuiElement, Svg, ContextFragment };
1011
export * from './smui.types.js';

packages/common/src/smui.types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ export type SmuiAttrs<
99
} & SvelteAttrs &
1010
DataAttrs;
1111

12+
export type SmuiSvgAttrs<OwnProps extends Object> = {
13+
[P in keyof Omit<
14+
svelte.JSX.SVGProps<SVGSVGElement>,
15+
keyof OwnProps
16+
>]?: svelte.JSX.SVGProps<SVGSVGElement>[P];
17+
} & SvelteAttrs &
18+
DataAttrs;
19+
1220
export type SvelteAttrs = {
1321
'sveltekit:noscroll'?: true | undefined | null;
1422
'sveltekit:prefetch'?: true | undefined | null;
@@ -173,8 +181,6 @@ export interface SmuiElementMap {
173181
// frameset: HTMLElementTagNameMap['frameset'];
174182
// marquee: HTMLElementTagNameMap['marquee'];
175183
template: HTMLElementTagNameMap['template'];
176-
177-
svg: SVGSVGElement;
178184
}
179185

180186
export interface SmuiElementPropMap {
@@ -295,6 +301,4 @@ export interface SmuiElementPropMap {
295301
// frameset: svelte.JSX.HTMLProps<HTMLElementTagNameMap['frameset']>;
296302
// marquee: svelte.JSX.HTMLProps<HTMLElementTagNameMap['marquee']>;
297303
template: svelte.JSX.HTMLProps<HTMLElementTagNameMap['template']>;
298-
299-
svg: svelte.JSX.SVGProps<SVGSVGElement>;
300304
}

packages/drawer/src/Scrim.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
classMap,
2424
dispatch,
2525
} from '@smui/common/internal';
26-
import type { SmuiElementMap, SmuiAttrs } from '@smui/common';
26+
import type { SmuiElementMap, SmuiAttrs, SmuiSvgAttrs } from '@smui/common';
2727
import { SmuiElement } from '@smui/common';
2828
2929
type TagName = $$Generic<keyof SmuiElementMap>;
@@ -35,7 +35,8 @@
3535
component?: Component;
3636
tag?: TagName;
3737
};
38-
type $$Props = OwnProps & SmuiAttrs<keyof SmuiElementMap, OwnProps>;
38+
type $$Props = OwnProps &
39+
(SmuiAttrs<keyof SmuiElementMap, OwnProps> | SmuiSvgAttrs<OwnProps>);
3940
4041
const forwardEvents = forwardEventsBuilder(get_current_component());
4142

packages/fab/src/Fab.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import type { ActionArray } from '@smui/common/internal';
4646
import { forwardEventsBuilder, classMap } from '@smui/common/internal';
4747
import Ripple from '@smui/ripple';
48-
import type { SmuiElementMap, SmuiAttrs } from '@smui/common';
48+
import type { SmuiElementMap, SmuiAttrs, SmuiSvgAttrs } from '@smui/common';
4949
import { SmuiElement } from '@smui/common';
5050
5151
type TagName = $$Generic<keyof SmuiElementMap>;
@@ -64,7 +64,8 @@
6464
component?: Component;
6565
tag?: TagName;
6666
};
67-
type $$Props = OwnProps & SmuiAttrs<keyof SmuiElementMap, OwnProps>;
67+
type $$Props = OwnProps &
68+
(SmuiAttrs<keyof SmuiElementMap, OwnProps> | SmuiSvgAttrs<OwnProps>);
6869
6970
const forwardEvents = forwardEventsBuilder(get_current_component());
7071

0 commit comments

Comments
 (0)