Time to embrace prefers-reduced-motion with picture

This commit is contained in:
Lim Chee Aun 2023-08-08 15:34:24 +08:00
parent fb798ce895
commit 58c6b6349c
2 changed files with 15 additions and 11 deletions

View file

@ -9,17 +9,21 @@ function EmojiText({ text, emojis }) {
const elements = text.split(regex).map((word) => { const elements = text.split(regex).map((word) => {
const emoji = emojis.find((e) => e.shortcode === word); const emoji = emojis.find((e) => e.shortcode === word);
if (emoji) { if (emoji) {
const { url, staticUrl } = emoji;
return ( return (
<img <picture>
key={word} <source srcset={staticUrl} media="(prefers-reduced-motion: reduce)" />
src={emoji.url} <img
alt={word} key={word}
class="shortcode-emoji emoji" src={url}
width="12" alt={word}
height="12" class="shortcode-emoji emoji"
loading="lazy" width="12"
decoding="async" height="12"
/> loading="lazy"
decoding="async"
/>
</picture>
); );
} }
return word; return word;

View file

@ -8,7 +8,7 @@ function emojifyText(text, emojis = []) {
const { shortcode, staticUrl, url } = emoji; const { shortcode, staticUrl, url } = emoji;
text = text.replace( text = text.replace(
new RegExp(`:${shortcode}:`, 'g'), new RegExp(`:${shortcode}:`, 'g'),
`<img class="shortcode-emoji emoji" src="${url}" alt=":${shortcode}:" width="12" height="12" loading="lazy" decoding="async" />`, `<picture><source srcset="${staticUrl}" media="(prefers-reduced-motion: reduce)"></source><img class="shortcode-emoji emoji" src="${url}" alt=":${shortcode}:" width="12" height="12" loading="lazy" decoding="async" /></picture>`,
); );
}); });
// console.log(text, emojis); // console.log(text, emojis);