|
42 | 42 | * @default 0
|
43 | 43 | */
|
44 | 44 | strokeWidth?: number;
|
45 |
| -
|
46 |
| - // Styling --- |
47 |
| - /** Set transform styles. */ |
48 |
| - transform?: string; |
49 |
| - /** Set fill styles. */ |
50 |
| - fill?: string; |
51 | 45 | }
|
52 | 46 | </script>
|
53 | 47 |
|
54 | 48 | <script lang="ts">
|
55 | 49 | import type { Alignment, Side } from "@floating-ui/dom";
|
56 | 50 | import { platform } from "@floating-ui/dom";
|
57 | 51 | import { useId } from "../../hooks/use-id.js";
|
58 |
| - import { styleObjectToString } from "../../internal/style-object-to-string.js"; |
| 52 | + import { |
| 53 | + styleObjectToString, |
| 54 | + styleStringToObject, |
| 55 | + } from "../../internal/style-object-to-string.js"; |
| 56 | + import parse from "style-to-object"; |
| 57 | + import { watch } from "../../internal/watch.svelte.js"; |
59 | 58 |
|
60 | 59 | let {
|
61 | 60 | ref = $bindable(null),
|
|
68 | 67 | staticOffset,
|
69 | 68 | stroke,
|
70 | 69 | d,
|
71 |
| - // --- |
72 |
| - transform, |
73 |
| - fill, |
74 |
| - // --- |
| 70 | + style: styleProp = "", |
75 | 71 | ...rest
|
76 | 72 | }: FloatingArrowProps = $props();
|
77 | 73 |
|
| 74 | + const { transform, ...restStyle } = $derived( |
| 75 | + styleStringToObject(styleProp) |
| 76 | + ); |
| 77 | +
|
78 | 78 | const clipPathId = useId();
|
79 | 79 |
|
| 80 | + let isRTL = $state(false); |
| 81 | +
|
| 82 | + // https://github.com/floating-ui/floating-ui/issues/2932 |
| 83 | + watch( |
| 84 | + () => context.elements.floating, |
| 85 | + (floatingEl) => { |
| 86 | + if (!floatingEl) return; |
| 87 | + if (getComputedStyle(floatingEl).direction === "rtl") { |
| 88 | + isRTL = true; |
| 89 | + } |
| 90 | + } |
| 91 | + ); |
| 92 | +
|
| 93 | + const [side, alignment] = $derived( |
| 94 | + context.placement.split("-") as [Side, Alignment] |
| 95 | + ); |
| 96 | + const isVerticalSide = $derived(side === "top" || side === "bottom"); |
| 97 | +
|
| 98 | + const computedStaticOffset = $derived.by(() => { |
| 99 | + if ( |
| 100 | + (isVerticalSide && context.middlewareData.shift?.x) || |
| 101 | + (!isVerticalSide && context.middlewareData.shift?.y) |
| 102 | + ) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + return staticOffset; |
| 106 | + }); |
| 107 | +
|
80 | 108 | // Strokes must be double the border width, this ensures the stroke's width
|
81 | 109 | // works as you'd expect.
|
82 | 110 | const computedStrokeWidth = $derived(strokeWidth * 2);
|
|
85 | 113 | const svgX = $derived((width / 2) * (tipRadius / -8 + 1));
|
86 | 114 | const svgY = $derived(((height / 2) * tipRadius) / 4);
|
87 | 115 |
|
88 |
| - const [side, alignment] = $derived( |
89 |
| - context.placement.split("-") as [Side, Alignment] |
90 |
| - ); |
91 |
| - const isRTL = $derived( |
92 |
| - context.elements.floating && platform.isRTL(context.elements.floating) |
93 |
| - ); |
94 | 116 | const isCustomShape = $derived(!!d);
|
95 |
| - const isVerticalSide = $derived(side === "top" || side === "bottom"); |
96 | 117 |
|
97 | 118 | const yOffsetProp = $derived(
|
98 |
| - staticOffset && alignment === "end" ? "bottom" : "top" |
| 119 | + computedStaticOffset && alignment === "end" ? "bottom" : "top" |
99 | 120 | );
|
100 | 121 | const xOffsetProp = $derived.by(() => {
|
101 |
| - if (!staticOffset) { |
102 |
| - return "left"; |
103 |
| - } |
104 |
| - if (isRTL) { |
105 |
| - return alignment === "end" ? "right" : "left"; |
| 122 | + if (computedStaticOffset && isRTL) { |
| 123 | + return alignment === "end" ? "left" : "right"; |
106 | 124 | }
|
107 | 125 | return alignment === "end" ? "right" : "left";
|
108 | 126 | });
|
109 | 127 |
|
110 | 128 | const arrowX = $derived(
|
111 |
| - arrow?.x != null ? staticOffset || `${arrow.x}px` : "" |
| 129 | + context.middlewareData.arrow?.x != null |
| 130 | + ? staticOffset || `${context.middlewareData.arrow.x}px` |
| 131 | + : "" |
112 | 132 | );
|
113 | 133 | const arrowY = $derived(
|
114 |
| - arrow?.y != null ? staticOffset || `${arrow.y}px` : "" |
| 134 | + context.middlewareData.arrow?.y != null |
| 135 | + ? staticOffset || `${context.middlewareData.arrow.y}px` |
| 136 | + : "" |
115 | 137 | );
|
116 | 138 |
|
117 | 139 | const dValue = $derived(
|
|
120 | 142 | );
|
121 | 143 |
|
122 | 144 | const rotation = $derived.by(() => {
|
123 |
| - switch (side) { |
124 |
| - case "top": |
125 |
| - return isCustomShape ? "rotate(180deg)" : ""; |
126 |
| - case "left": |
127 |
| - return isCustomShape ? "rotate(90deg)" : "rotate(-90deg)"; |
128 |
| - case "bottom": |
129 |
| - return isCustomShape ? "" : "rotate(180deg)"; |
130 |
| - case "right": |
131 |
| - return isCustomShape ? "rotate(-90deg)" : "rotate(90deg)"; |
132 |
| - } |
| 145 | + return { |
| 146 | + top: isCustomShape ? "rotate(180deg)" : "", |
| 147 | + left: isCustomShape ? "rotate(90deg)" : "rotate(-90deg)", |
| 148 | + bottom: isCustomShape ? "" : "rotate(180deg)", |
| 149 | + right: isCustomShape ? "rotate(-90deg)" : "rotate(90deg)", |
| 150 | + }[side]; |
133 | 151 | });
|
134 | 152 | </script>
|
135 | 153 |
|
136 | 154 | <!-- FIXME: extend styleObjectToString type to accept `rest.styles` -->
|
137 | 155 |
|
138 |
| -<svg |
139 |
| - bind:this={ref} |
140 |
| - width={isCustomShape ? width : width + computedStrokeWidth} |
141 |
| - height={width} |
142 |
| - viewBox={`0 0 ${width} ${height > width ? height : width}`} |
143 |
| - aria-hidden="true" |
144 |
| - style={styleObjectToString({ |
145 |
| - position: "absolute", |
146 |
| - "pointer-events": "none", |
147 |
| - [xOffsetProp]: `${arrowX}`, |
148 |
| - [yOffsetProp]: `${arrowY}`, |
149 |
| - [side]: |
150 |
| - isVerticalSide || isCustomShape |
151 |
| - ? "100%" |
152 |
| - : `calc(100% - ${computedStrokeWidth / 2}px)`, |
153 |
| - transform: `${rotation} ${transform ?? ""}`, |
154 |
| - fill, |
155 |
| - })} |
156 |
| - data-testid="floating-arrow" |
157 |
| - {...rest}> |
158 |
| - {#if computedStrokeWidth > 0} |
159 |
| - <!-- Account for the stroke on the fill path rendered below. --> |
160 |
| - <path |
161 |
| - fill="none" |
162 |
| - {stroke} |
163 |
| - clip-path={`url(#${clipPathId})`} |
164 |
| - stroke-width={computedStrokeWidth + (d ? 0 : 1)} |
165 |
| - d={dValue} /> |
166 |
| - {/if} |
167 |
| - <!-- |
| 156 | +{#if context.elements.floating} |
| 157 | + <svg |
| 158 | + bind:this={ref} |
| 159 | + {...rest} |
| 160 | + width={isCustomShape ? width : width + computedStrokeWidth} |
| 161 | + height={width} |
| 162 | + viewBox={`0 0 ${width} ${height > width ? height : width}`} |
| 163 | + aria-hidden="true" |
| 164 | + style={styleObjectToString({ |
| 165 | + position: "absolute", |
| 166 | + "pointer-events": "none", |
| 167 | + [xOffsetProp]: `${arrowX}`, |
| 168 | + [yOffsetProp]: `${arrowY}`, |
| 169 | + [side]: |
| 170 | + isVerticalSide || isCustomShape |
| 171 | + ? "100%" |
| 172 | + : `calc(100% - ${computedStrokeWidth / 2}px)`, |
| 173 | + transform: [rotation, transform].filter((t) => !!t).join(" "), |
| 174 | + ...restStyle, |
| 175 | + })} |
| 176 | + data-testid="floating-arrow"> |
| 177 | + {#if computedStrokeWidth > 0} |
| 178 | + <!-- Account for the stroke on the fill path rendered below. --> |
| 179 | + <path |
| 180 | + clip-path={`url(#${clipPathId})`} |
| 181 | + fill="none" |
| 182 | + {stroke} |
| 183 | + stroke-width={computedStrokeWidth + (d ? 0 : 1)} |
| 184 | + d={dValue} /> |
| 185 | + {/if} |
| 186 | + <!-- |
168 | 187 | In Firefox, for left/right placements there's a ~0.5px gap where the
|
169 | 188 | border can show through. Adding a stroke on the fill removes it.
|
170 | 189 | -->
|
171 |
| - <path stroke={computedStrokeWidth && !d ? fill : "none"} d={dValue} /> |
172 |
| - <!-- Assumes the border-width of the floating element matches the stroke. --> |
173 |
| - <clipPath id={clipPathId}> |
174 |
| - <rect |
175 |
| - x={-halfStrokeWidth} |
176 |
| - y={halfStrokeWidth * (isCustomShape ? -1 : 1)} |
177 |
| - width={width + computedStrokeWidth} |
178 |
| - height={width} /> |
179 |
| - </clipPath> |
180 |
| -</svg> |
| 190 | + <path |
| 191 | + stroke={computedStrokeWidth && !d ? rest.fill : "none"} |
| 192 | + d={dValue} /> |
| 193 | + <!-- Assumes the border-width of the floating element matches the stroke. --> |
| 194 | + <clipPath id={clipPathId}> |
| 195 | + <rect |
| 196 | + x={-halfStrokeWidth} |
| 197 | + y={halfStrokeWidth * (isCustomShape ? -1 : 1)} |
| 198 | + width={width + computedStrokeWidth} |
| 199 | + height={width} /> |
| 200 | + </clipPath> |
| 201 | + </svg> |
| 202 | +{/if} |
0 commit comments