-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve link sharing by using an icon (#175)
Solves #138 Also squashes a small bug in the lots-toggle-{on,off} button.
- Loading branch information
1 parent
1bb5aa9
commit f5dd0c1
Showing
6 changed files
with
70 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
@use "about"; | ||
@use "controls"; | ||
@use "copied-link"; | ||
@use "donate"; | ||
@use "header"; | ||
@use "scorecard"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* global document, navigator, window */ | ||
import { determineShareUrl } from "./cityId"; | ||
/** | ||
* Copy `value` to the user's clipboard | ||
* | ||
* @param string value | ||
*/ | ||
const copyToClipboard = async (value) => { | ||
try { | ||
await navigator.clipboard.writeText(value); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error("Failed to write to clipboard: ", err); | ||
} | ||
}; | ||
|
||
/** | ||
* Toggle share link icon briefly to show user an indicator | ||
* | ||
* @param {HTMLAnchorElement} shareIcon | ||
*/ | ||
const switchIcons = (shareIcon) => { | ||
const linkIcon = shareIcon.querySelector("svg.share-link-icon"); | ||
const checkIcon = shareIcon.querySelector("svg.share-check-icon"); | ||
linkIcon.style.display = "none"; | ||
checkIcon.style.display = "inline-block"; | ||
setTimeout(() => { | ||
linkIcon.style.display = "inline-block"; | ||
checkIcon.style.display = "none"; | ||
}, 1000); | ||
}; | ||
|
||
/** | ||
* Add an event listener for the share button to copy the link to the clipboard. | ||
* | ||
* @param string cityId: e.g. `st.-louis-mo` | ||
*/ | ||
const setUpShareUrlClickListener = (cityId) => { | ||
// We put the event listener on `map` because it is never erased, unlike the copy button | ||
// being recreated every time the score card changes. This is called "event delegation". | ||
document.querySelector("#map").addEventListener("click", async (event) => { | ||
const targetElement = event.target.closest("div.url-copy-button > a"); | ||
if (targetElement) { | ||
const shareUrl = determineShareUrl(window.location.href, cityId); | ||
await copyToClipboard(shareUrl); | ||
switchIcons(targetElement); | ||
} | ||
}); | ||
}; | ||
|
||
export default setUpShareUrlClickListener; |