Skip to content

Commit da09e0b

Browse files
authored
[polaris.shopify.com] Update browser url when copying url (#10573)
Had a bit of time so decided to slightly improve the copy url button to also change the browser url. This behavior is more similar to how other sites, like github, do this https://github.com/Shopify/polaris/assets/6844391/7990d44a-fe30-4a73-8735-c0d08fcc9e19 I initially did this with window.location.replace but that jumps the page around and scrolls to the anchor which messed with the tooltip messaging too much and felt janky
1 parent ea6b542 commit da09e0b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

.changeset/ten-snakes-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'polaris.shopify.com': patch
3+
---
4+
5+
Updated copy url to change browser url

polaris.shopify.com/src/components/Markdown/Markdown.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ function HeadingWithCopyButton({
155155
? window.location.origin
156156
: 'https://polaris.shopify.com';
157157
const path = typeof window !== 'undefined' ? window.location.pathname : '';
158-
const [copy, didJustCopy] = useCopyToClipboard(`${origin}${path}#${id}`);
158+
const url = `${origin}${path}#${id}`;
159+
const [copy, didJustCopy] = useCopyToClipboard(url);
159160

160161
return (
161162
<Element id={id} className={styles.MarkdownHeading}>
@@ -164,7 +165,15 @@ function HeadingWithCopyButton({
164165
ariaLabel="Copy to clipboard"
165166
renderContent={() => <p>{didJustCopy ? 'Copied' : 'Copy'}</p>}
166167
>
167-
<button className={styles.MarkdownCopyButton} onClick={copy}>
168+
<button
169+
className={styles.MarkdownCopyButton}
170+
onClick={() => {
171+
copy();
172+
if (typeof window !== 'undefined') {
173+
window.history.pushState(id, '', url);
174+
}
175+
}}
176+
>
168177
<Icon source={ClipboardMinor} width={16} height={16} />
169178
</button>
170179
</Tooltip>

0 commit comments

Comments
 (0)