Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV-12552] Feature - Show pencil next to the caption & truncate if needed #530

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import "styles/helpers";
@import "styles/variables";

.GalleryTile {
Expand Down Expand Up @@ -82,6 +83,7 @@

.Input {
width: 100%;
padding: 0;
appearance: none;
color: $white;
background: none;
Expand All @@ -97,6 +99,43 @@
font-weight: 300;
}
}

.Button {
max-width: 100%;
margin: 0;
padding: 0;
border: none;
font-size: 13px;
font-weight: 500;
line-height: 1.2;
overflow: hidden;

&,
&:hover,
&:focus {
color: $white !important;
background: none !important;
}

&.empty {
font-style: italic;
font-weight: 300;
}

> div {
overflow: hidden;
}

span {
@include ellipsis;

opacity: 1;
}

svg {
flex-shrink: 0;
}
}
}

.Actions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import classNames from 'classnames';
import type { CSSProperties } from 'react';
import React, { forwardRef, useState } from 'react';
import type { CSSProperties, ChangeEvent } from 'react';
import React, { forwardRef, useEffect, useState } from 'react';

import { Button, ImageSizeWarning, ImageWithLoadingPlaceholder } from '#components';
import { Crop, Delete } from '#icons';
import { Crop, Delete, Edit } from '#icons';

import styles from './GalleryTile.module.scss';

Expand All @@ -28,7 +28,7 @@ export interface Props {

export const GalleryTile = forwardRef<HTMLDivElement, Props>(function GalleryTile(
{
caption,
caption: originalCaption,
className,
clone = false,
dragging,
Expand All @@ -48,7 +48,18 @@ export const GalleryTile = forwardRef<HTMLDivElement, Props>(function GalleryTil
},
ref,
) {
// We have to use an intermediate state otherwise the input keeps
// re-rendering every time the original caption changes and moves
// the caret to the end of the input
const [caption, setCaption] = useState(originalCaption);
const [isHovering, setHovering] = useState(false);
const [isEditingCaption, setEditingCaption] = useState(false);

function handleCaptionChange(event: ChangeEvent<HTMLInputElement>) {
const text = event.currentTarget.value;
setCaption(text);
onCaptionChange(text);
}

function handleShowOverlay() {
setTimeout(() => setHovering(true), 0);
Expand All @@ -58,6 +69,10 @@ export const GalleryTile = forwardRef<HTMLDivElement, Props>(function GalleryTil
setHovering(false);
}

useEffect(() => {
setCaption(caption);
}, [caption]);

return (
<div
className={classNames(styles.GalleryTile, className, {
Expand Down Expand Up @@ -88,13 +103,29 @@ export const GalleryTile = forwardRef<HTMLDivElement, Props>(function GalleryTil
[styles.visible]: caption !== '',
})}
>
<input
type="text"
className={styles.Input}
onChange={(event) => onCaptionChange?.(event.currentTarget.value)}
value={caption}
placeholder={isInteractive ? 'add caption' : ''}
/>
{isEditingCaption ? (
<input
autoFocus
type="text"
className={styles.Input}
onChange={handleCaptionChange}
onBlur={() => setEditingCaption(false)}
value={caption}
placeholder="add caption"
/>
) : (
<Button
className={classNames(styles.Button, {
[styles.empty]: !caption,
})}
icon={isInteractive ? Edit : undefined}
iconPosition="right"
onClick={() => setEditingCaption(true)}
variant="clear"
>
{caption || 'add caption'}
</Button>
)}
</div>
</div>
)}
Expand Down
Loading