Add ability to view alt text by clicking the ALT badge in web UI (#32058)

This commit is contained in:
Eugen Rochko 2024-09-26 15:26:49 +02:00 committed by GitHub
parent 7a62d57427
commit a04433f995
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 121 additions and 16 deletions

View file

@ -0,0 +1,67 @@
import { useState, useCallback, useRef } from 'react';
import { FormattedMessage } from 'react-intl';
import Overlay from 'react-overlays/Overlay';
import type {
OffsetValue,
UsePopperOptions,
} from 'react-overlays/esm/usePopper';
const offset = [0, 4] as OffsetValue;
const popperConfig = { strategy: 'fixed' } as UsePopperOptions;
export const AltTextBadge: React.FC<{
description: string;
}> = ({ description }) => {
const anchorRef = useRef<HTMLButtonElement>(null);
const [open, setOpen] = useState(false);
const handleClick = useCallback(() => {
setOpen((v) => !v);
}, [setOpen]);
const handleClose = useCallback(() => {
setOpen(false);
}, [setOpen]);
return (
<>
<button
ref={anchorRef}
className='media-gallery__alt__label'
onClick={handleClick}
>
ALT
</button>
<Overlay
rootClose
onHide={handleClose}
show={open}
target={anchorRef.current}
placement='top-end'
flip
offset={offset}
popperConfig={popperConfig}
>
{({ props }) => (
<div {...props} className='hover-card-controller'>
<div
className='media-gallery__alt__popover dropdown-animation'
role='tooltip'
>
<h4>
<FormattedMessage
id='alt_text_badge.title'
defaultMessage='Alt text'
/>
</h4>
<p>{description}</p>
</div>
</div>
)}
</Overlay>
</>
);
};

View file

@ -10,6 +10,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
import { debounce } from 'lodash';
import { AltTextBadge } from 'mastodon/components/alt_text_badge';
import { Blurhash } from 'mastodon/components/blurhash';
import { formatTime } from 'mastodon/features/video';
@ -97,7 +98,7 @@ class Item extends PureComponent {
}
if (attachment.get('description')?.length > 0) {
badges.push(<span key='alt' className='media-gallery__alt__label'>ALT</span>);
badges.push(<AltTextBadge key='alt' description={attachment.get('description')} />);
}
const description = attachment.getIn(['translation', 'description']) || attachment.get('description');
@ -156,9 +157,9 @@ class Item extends PureComponent {
const duration = attachment.getIn(['meta', 'original', 'duration']);
if (attachment.get('type') === 'gifv') {
badges.push(<span key='gif' className='media-gallery__gifv__label'>GIF</span>);
badges.push(<span key='gif' className='media-gallery__alt__label media-gallery__alt__label--non-interactive'>GIF</span>);
} else {
badges.push(<span key='video' className='media-gallery__gifv__label'>{formatTime(Math.floor(duration))}</span>);
badges.push(<span key='video' className='media-gallery__alt__label media-gallery__alt__label--non-interactive'>{formatTime(Math.floor(duration))}</span>);
}
thumbnail = (

View file

@ -5,6 +5,7 @@ import classNames from 'classnames';
import HeadphonesIcon from '@/material-icons/400-24px/headphones-fill.svg?react';
import MovieIcon from '@/material-icons/400-24px/movie-fill.svg?react';
import VisibilityOffIcon from '@/material-icons/400-24px/visibility_off.svg?react';
import { AltTextBadge } from 'mastodon/components/alt_text_badge';
import { Blurhash } from 'mastodon/components/blurhash';
import { Icon } from 'mastodon/components/icon';
import { formatTime } from 'mastodon/features/video';
@ -77,11 +78,7 @@ export const MediaItem: React.FC<{
const badges = [];
if (description && description.length > 0) {
badges.push(
<span key='alt' className='media-gallery__alt__label'>
ALT
</span>,
);
badges.push(<AltTextBadge key='alt' description={description} />);
}
if (!visible) {
@ -156,13 +153,19 @@ export const MediaItem: React.FC<{
if (type === 'gifv') {
badges.push(
<span key='gif' className='media-gallery__gifv__label'>
<span
key='gif'
className='media-gallery__alt__label media-gallery__alt__label--non-interactive'
>
GIF
</span>,
);
} else {
badges.push(
<span key='video' className='media-gallery__gifv__label'>
<span
key='video'
className='media-gallery__alt__label media-gallery__alt__label--non-interactive'
>
{formatTime(Math.floor(duration))}
</span>,
);

View file

@ -85,6 +85,7 @@
"alert.rate_limited.title": "Rate limited",
"alert.unexpected.message": "An unexpected error occurred.",
"alert.unexpected.title": "Oops!",
"alt_text_badge.title": "Alt text",
"announcement.announcement": "Announcement",
"attachments_list.unprocessed": "(unprocessed)",
"audio.hide": "Hide audio",

View file

@ -6971,14 +6971,14 @@ a.status-card {
inset-inline-end: 8px;
display: flex;
gap: 2px;
pointer-events: none;
}
.media-gallery__alt__label,
.media-gallery__gifv__label {
display: flex;
align-items: center;
justify-content: center;
.media-gallery__alt__label {
display: block;
text-align: center;
color: $white;
border: 0;
background: rgba($black, 0.65);
backdrop-filter: blur(10px) saturate(180%) contrast(75%) brightness(70%);
padding: 3px 8px;
@ -6986,8 +6986,41 @@ a.status-card {
font-size: 12px;
font-weight: 700;
z-index: 1;
pointer-events: none;
line-height: 20px;
cursor: pointer;
pointer-events: auto;
&--non-interactive {
pointer-events: none;
}
}
.media-gallery__alt__popover {
background: rgba($black, 0.65);
backdrop-filter: blur(10px) saturate(180%) contrast(75%) brightness(70%);
border-radius: 4px;
box-shadow: var(--dropdown-shadow);
padding: 16px;
min-width: 16em;
min-height: 2em;
max-width: 22em;
max-height: 30em;
overflow-y: auto;
h4 {
font-size: 15px;
line-height: 20px;
font-weight: 500;
color: $white;
margin-bottom: 8px;
}
p {
font-size: 15px;
line-height: 20px;
color: rgba($white, 0.85);
white-space: pre-line;
}
}
.attachment-list {