Add ability to view alt text by clicking the ALT badge in web UI (#32058)
This commit is contained in:
		
							parent
							
								
									7a62d57427
								
							
						
					
					
						commit
						a04433f995
					
				
							
								
								
									
										67
									
								
								app/javascript/mastodon/components/alt_text_badge.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								app/javascript/mastodon/components/alt_text_badge.tsx
									
									
									
									
									
										Normal 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>
 | 
				
			||||||
 | 
					    </>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
@ -10,6 +10,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import { debounce } from 'lodash';
 | 
					import { debounce } from 'lodash';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { AltTextBadge } from 'mastodon/components/alt_text_badge';
 | 
				
			||||||
import { Blurhash } from 'mastodon/components/blurhash';
 | 
					import { Blurhash } from 'mastodon/components/blurhash';
 | 
				
			||||||
import { formatTime } from 'mastodon/features/video';
 | 
					import { formatTime } from 'mastodon/features/video';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -97,7 +98,7 @@ class Item extends PureComponent {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (attachment.get('description')?.length > 0) {
 | 
					    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');
 | 
					    const description = attachment.getIn(['translation', 'description']) || attachment.get('description');
 | 
				
			||||||
@ -156,9 +157,9 @@ class Item extends PureComponent {
 | 
				
			|||||||
      const duration = attachment.getIn(['meta', 'original', 'duration']);
 | 
					      const duration = attachment.getIn(['meta', 'original', 'duration']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (attachment.get('type') === 'gifv') {
 | 
					      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 {
 | 
					      } 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 = (
 | 
					      thumbnail = (
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,7 @@ import classNames from 'classnames';
 | 
				
			|||||||
import HeadphonesIcon from '@/material-icons/400-24px/headphones-fill.svg?react';
 | 
					import HeadphonesIcon from '@/material-icons/400-24px/headphones-fill.svg?react';
 | 
				
			||||||
import MovieIcon from '@/material-icons/400-24px/movie-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 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 { Blurhash } from 'mastodon/components/blurhash';
 | 
				
			||||||
import { Icon } from 'mastodon/components/icon';
 | 
					import { Icon } from 'mastodon/components/icon';
 | 
				
			||||||
import { formatTime } from 'mastodon/features/video';
 | 
					import { formatTime } from 'mastodon/features/video';
 | 
				
			||||||
@ -77,11 +78,7 @@ export const MediaItem: React.FC<{
 | 
				
			|||||||
  const badges = [];
 | 
					  const badges = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (description && description.length > 0) {
 | 
					  if (description && description.length > 0) {
 | 
				
			||||||
    badges.push(
 | 
					    badges.push(<AltTextBadge key='alt' description={description} />);
 | 
				
			||||||
      <span key='alt' className='media-gallery__alt__label'>
 | 
					 | 
				
			||||||
        ALT
 | 
					 | 
				
			||||||
      </span>,
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!visible) {
 | 
					  if (!visible) {
 | 
				
			||||||
@ -156,13 +153,19 @@ export const MediaItem: React.FC<{
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (type === 'gifv') {
 | 
					    if (type === 'gifv') {
 | 
				
			||||||
      badges.push(
 | 
					      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
 | 
					          GIF
 | 
				
			||||||
        </span>,
 | 
					        </span>,
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      badges.push(
 | 
					      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))}
 | 
					          {formatTime(Math.floor(duration))}
 | 
				
			||||||
        </span>,
 | 
					        </span>,
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
 | 
				
			|||||||
@ -85,6 +85,7 @@
 | 
				
			|||||||
  "alert.rate_limited.title": "Rate limited",
 | 
					  "alert.rate_limited.title": "Rate limited",
 | 
				
			||||||
  "alert.unexpected.message": "An unexpected error occurred.",
 | 
					  "alert.unexpected.message": "An unexpected error occurred.",
 | 
				
			||||||
  "alert.unexpected.title": "Oops!",
 | 
					  "alert.unexpected.title": "Oops!",
 | 
				
			||||||
 | 
					  "alt_text_badge.title": "Alt text",
 | 
				
			||||||
  "announcement.announcement": "Announcement",
 | 
					  "announcement.announcement": "Announcement",
 | 
				
			||||||
  "attachments_list.unprocessed": "(unprocessed)",
 | 
					  "attachments_list.unprocessed": "(unprocessed)",
 | 
				
			||||||
  "audio.hide": "Hide audio",
 | 
					  "audio.hide": "Hide audio",
 | 
				
			||||||
 | 
				
			|||||||
@ -6971,14 +6971,14 @@ a.status-card {
 | 
				
			|||||||
  inset-inline-end: 8px;
 | 
					  inset-inline-end: 8px;
 | 
				
			||||||
  display: flex;
 | 
					  display: flex;
 | 
				
			||||||
  gap: 2px;
 | 
					  gap: 2px;
 | 
				
			||||||
 | 
					  pointer-events: none;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.media-gallery__alt__label,
 | 
					.media-gallery__alt__label {
 | 
				
			||||||
.media-gallery__gifv__label {
 | 
					  display: block;
 | 
				
			||||||
  display: flex;
 | 
					  text-align: center;
 | 
				
			||||||
  align-items: center;
 | 
					 | 
				
			||||||
  justify-content: center;
 | 
					 | 
				
			||||||
  color: $white;
 | 
					  color: $white;
 | 
				
			||||||
 | 
					  border: 0;
 | 
				
			||||||
  background: rgba($black, 0.65);
 | 
					  background: rgba($black, 0.65);
 | 
				
			||||||
  backdrop-filter: blur(10px) saturate(180%) contrast(75%) brightness(70%);
 | 
					  backdrop-filter: blur(10px) saturate(180%) contrast(75%) brightness(70%);
 | 
				
			||||||
  padding: 3px 8px;
 | 
					  padding: 3px 8px;
 | 
				
			||||||
@ -6986,8 +6986,41 @@ a.status-card {
 | 
				
			|||||||
  font-size: 12px;
 | 
					  font-size: 12px;
 | 
				
			||||||
  font-weight: 700;
 | 
					  font-weight: 700;
 | 
				
			||||||
  z-index: 1;
 | 
					  z-index: 1;
 | 
				
			||||||
  pointer-events: none;
 | 
					 | 
				
			||||||
  line-height: 20px;
 | 
					  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 {
 | 
					.attachment-list {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user