Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

gif_picker: Enable resize for GIF picker.

Aman Agrawalbf6f179398b4
diff --git a/web/src/gif_picker_ui.ts b/web/src/gif_picker_ui.ts
index 0533181e76a..b1bce786bd0 100644
--- a/web/src/gif_picker_ui.ts
+++ b/web/src/gif_picker_ui.ts
@@ -7,14 +7,19 @@ import render_gif_picker_gif from "../templates/gif_picker_gif.hbs";
import render_no_gif_results from "../templates/no_gif_results.hbs";
import type {GifInfoUrl, GifNetwork} from "./abstract_gif_network.ts";
+import {make_resizable} from "./box_resize.ts";
import {ComposeIconSession} from "./compose_icon_session.ts";
import * as gif_picker_popover_content from "./gif_picker_popover_content.ts";
import * as gif_state from "./gif_state.ts";
import * as giphy_network from "./giphy_network.ts";
import * as klipy_network from "./klipy_network.ts";
+import * as modals from "./modals.ts";
+import * as overlay_util from "./overlay_util.ts";
+import * as overlays from "./overlays.ts";
import * as popover_menus from "./popover_menus.ts";
import * as scroll_util from "./scroll_util.ts";
import * as tenor_network from "./tenor_network.ts";
+import * as util from "./util.ts";
// Only used if popover called from edit message, otherwise it is `undefined`.
let compose_icon_session: ComposeIconSession | undefined;
@@ -23,6 +28,7 @@ let current_search_term: undefined | string;
// Stores the index of the last GIF that is part of the grid.
let last_gif_index = -1;
let network: GifNetwork;
+let resizable_grid_cleanup: (() => void) | undefined;
function is_editing_existing_message(): boolean {
if (compose_icon_session === undefined) {
@@ -146,6 +152,7 @@ function render_gifs_to_grid(urls: GifInfoUrl[], next_page: boolean): void {
if (urls.length === 0) {
const no_gif_results_html = render_no_gif_results();
$popper.find(".gif-picker-content").html(no_gif_results_html);
+ recenter_overlay();
return;
}
}
@@ -163,9 +170,18 @@ function render_gifs_to_grid(urls: GifInfoUrl[], next_page: boolean): void {
} else {
$popper.find(".gif-scrolling-container .simplebar-content-wrapper").scrollTop(0);
$popper.find(".gif-picker-content").html(gif_grid_html);
+ // On the initial render, Tippy positioned the popover before
+ // the grid's `height: 70dvh` had settled, so the `translate3d`
+ // is computed against a smaller box and the final-sized box
+ // overflows at the bottom. Re-run popper with the final size.
+ recenter_overlay();
}
}
+function recenter_overlay(): void {
+ void popover_instance?.popperInstance?.update();
+}
+
function render_featured_gifs(next_page: boolean): void {
if (
network.is_loading_more_gifs() ||
@@ -205,21 +221,6 @@ function toggle_picker_popover(target: HTMLElement): void {
target,
{
theme: "popover-menu",
- placement: "top",
- popperOptions: {
- modifiers: [
- {
- // The placement is set to top by default, and we use
- // bottom and left configurations as fallback, which is
- // useful for scenarios when opening the picker while editing
- // messages near the top of the viewport.
- name: "flip",
- options: {
- fallbackPlacements: ["bottom", "left"],
- },
- },
- ],
- },
onCreate(instance) {
const provider = network.get_provider();
instance.setContent(gif_picker_popover_content.get_gif_popover_content(provider));
@@ -229,6 +230,12 @@ function toggle_picker_popover(target: HTMLElement): void {
},
onShow(instance) {
popover_instance = instance;
+ // Clicking the compose GIF icon outside an overlay or
+ // modal would close the overlay/modal first, so we
+ // should never show the picker with one open.
+ assert(!overlays.any_active());
+ assert(!modals.any_active());
+ overlay_util.disable_scrolling();
const $popper = $(instance.popper).trigger("focus");
const debounced_search = _.debounce((search_term: string) => {
update_grid_with_search_term(search_term);
@@ -253,6 +260,34 @@ function toggle_picker_popover(target: HTMLElement): void {
$popper.on("keydown", ".gif-picker-gif", handle_keyboard_navigation_on_gif);
},
onMount(instance) {
+ const grid = instance.popper.querySelector<HTMLElement>(".gif-grid-in-popover")!;
+ // On mobile devices, the picker fills the viewport and
+ // resize is disabled — 10px touch targets are too small
+ // for fingers, and there's no real estate to gain. We
+ // gate on device class (userAgent) rather than viewport
+ // width so a desktop user with a narrow window still
+ // gets the resizable picker.
+ if (util.is_mobile()) {
+ grid.classList.add("is-mobile-device");
+ } else {
+ resizable_grid_cleanup = make_resizable(
+ grid,
+ [
+ "top",
+ "right",
+ "bottom",
+ "left",
+ "top_left",
+ "top_right",
+ "bottom_left",
+ "bottom_right",
+ ],
+ // Re-center the overlay against the new size.
+ () => {
+ void instance.popperInstance?.update();
+ },
+ );
+ }
render_featured_gifs(false);
const $popper = $(instance.popper);
$popper.find("#gif-search-query").trigger("focus");
@@ -280,11 +315,16 @@ function toggle_picker_popover(target: HTMLElement): void {
},
onHidden() {
hide_picker_popover();
+ resizable_grid_cleanup?.();
+ resizable_grid_cleanup = undefined;
+ overlay_util.enable_scrolling();
},
},
{
show_as_overlay_on_mobile: true,
- show_as_overlay_always: false,
+ // A centered overlay sidesteps popper-anchor edge cases near
+ // the viewport top, and lets resize work symmetrically.
+ show_as_overlay_always: true,
},
);
}
diff --git a/web/styles/app_variables.css b/web/styles/app_variables.css
index 2a7979a5f51..963e03b062c 100644
--- a/web/styles/app_variables.css
+++ b/web/styles/app_variables.css
@@ -175,11 +175,51 @@
area. Consumed by box_resize.css. */
--resizable-box-handle-size: 10px;
- --gif-scroll-container-height: 80vh;
+ /* GIF picker overlay: resize bounds are expressed in columns so
+ the picker resizes to useful sizes. Tweaking these retunes the
+ bounds, grid columns, and gap together. */
+ --gif-picker-gif-column-min-width: 200px;
+ --gif-picker-gif-column-gap: 6px;
+ /* Horizontal chrome inside .gif-grid-in-popover around the grid
+ columns, measured against its border-box. Breakdown: 3px × 2
+ .gif-scrolling-container margin, 2px × 2 .gif-picker-content
+ padding, and handle-size padding on each side reserved for
+ resize handles. SimpleBar's scrollbar is a positioned overlay
+ and doesn't take content width, so nothing extra is reserved
+ for it. Keep in sync with popovers.css. */
+ --gif-picker-grid-horizontal-chrome: calc(
+ 10px + 2 * var(--resizable-box-handle-size)
+ );
+ --gif-picker-viewport-safe-margin: 20px;
+
+ /* Min = 1 column. Max = 5 columns, capped at viewport minus safe
+ margins. */
+ --gif-picker-grid-min-width: calc(
+ var(--gif-picker-gif-column-min-width) +
+ var(--gif-picker-grid-horizontal-chrome)
+ );
+ --gif-picker-grid-max-width: min(
+ calc(
+ 5 * var(--gif-picker-gif-column-min-width) + 4 *
+ var(--gif-picker-gif-column-gap) +
+ var(--gif-picker-grid-horizontal-chrome)
+ ),
+ calc(100vw - 2 * var(--gif-picker-viewport-safe-margin))
+ );
+ /* Room for the search input, one row of GIFs, the footer, and
+ the outer handle-size padding on the top and bottom. */
+ --gif-picker-grid-min-height: calc(
+ 300px + 2 * var(--resizable-box-handle-size)
+ );
+ --gif-picker-grid-max-height: calc(
+ 100vh - 2 * var(--gif-picker-viewport-safe-margin)
+ );
@supports (height: 100dvh) {
--full-height-dynamic-viewport: 100dvh;
- --gif-scroll-container-height: 80dvh;
+ --gif-picker-grid-max-height: calc(
+ 100dvh - 2 * var(--gif-picker-viewport-safe-margin)
+ );
}
/*
diff --git a/web/styles/popovers.css b/web/styles/popovers.css
index 56df860f612..4a6fd083cd0 100644
--- a/web/styles/popovers.css
+++ b/web/styles/popovers.css
@@ -498,13 +498,47 @@ ul.popover-group-menu-member-list {
}
.gif-grid-in-popover {
- width: 90vw;
- /* 6px `.gif-scroll-container` horizontal margin + 6px is the extra gutter space
- + 4px padding between gif columns. Our max-width is calculated based on this so the
- max width of the GIFs becomes 250px */
- max-width: 516px;
+ /* Anchor resize handles to this element's padding box so they
+ stay inside the popover. */
+ position: relative;
+ /* So width / min-width / max-width refer to the total visible
+ box, including the handle-size padding added below; the viewport
+ safe-margin caps then compare apples-to-apples. */
+ box-sizing: border-box;
+ /* Initial width: four columns. On narrow viewports, `max-width`
+ caps this at what fits. Users can drag between one and five
+ columns. */
+ width: calc(
+ 4 * var(--gif-picker-gif-column-min-width) + 3 *
+ var(--gif-picker-gif-column-gap) +
+ var(--gif-picker-grid-horizontal-chrome)
+ );
+ height: 70dvh;
+ min-width: var(--gif-picker-grid-min-width);
+ min-height: var(--gif-picker-grid-min-height);
+ max-width: var(--gif-picker-grid-max-width);
+ max-height: var(--gif-picker-grid-max-height);
+
+ /* On mobile devices (userAgent-based, see gif_picker_ui.ts), fill
+ the viewport and drop the handle-size padding since resize is
+ disabled. Gating on device class rather than viewport width
+ lets a desktop user on a narrow window keep the resizable
+ picker. */
+ &.is-mobile-device {
+ width: calc(100vw - 2 * var(--gif-picker-viewport-safe-margin));
+ height: calc(100dvh - 2 * var(--gif-picker-viewport-safe-margin));
+ min-width: 0;
+ min-height: 0;
+ max-width: none;
+ max-height: none;
+ padding: 0;
+ }
+
border: 0;
- padding: 0;
+ /* Reserve a strip equal to the resize handle size on every side
+ so the handles don't overlap clickable GIFs, the search input,
+ or the inner scrollbar. */
+ padding: var(--resizable-box-handle-size);
.gif-search-container {
margin: 4px 4px 2px;
@@ -519,9 +553,19 @@ ul.popover-group-menu-member-list {
.gif-picker-content {
display: grid;
- grid-template-columns: repeat(2, minmax(0, 1fr));
+ /* Reflow to more columns as the picker enlarges, using the
+ same min column width as the container resize bounds. */
+ grid-template-columns: repeat(
+ auto-fit,
+ minmax(var(--gif-picker-gif-column-min-width), 1fr)
+ );
grid-auto-rows: auto;
- gap: 6px;
+ gap: var(--gif-picker-gif-column-gap);
+
+ @media (width < $md_min) {
+ /* Allow narrower columns on mobile so two can fit on a phone. */
+ grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
+ }
/* Meant to prevent outline overflow when the GIF
is focused. */
@@ -547,23 +591,55 @@ ul.popover-group-menu-member-list {
}
}
- .gif-scrolling-container {
- overflow: hidden auto;
- height: var(--gif-scroll-container-height);
- max-height: 500px;
- margin: 2px 3px;
- padding: 5px 0;
+ .popover-inner {
+ /* Fill .gif-grid-in-popover so the scrolling container takes
+ the space left over after the search input. */
+ height: 100%;
+ display: flex;
+ flex-direction: column;
+
+ .gif-scrolling-container {
+ flex: 1 1 auto;
+ /* `min-height: 0` lets this flex item shrink below its
+ content's intrinsic height, so the scroll area can
+ be shorter than the grid it contains. */
+ min-height: 0;
+ /* SimpleBar's `.simplebar-wrapper` uses `height: inherit`
+ to pick up its host's *computed* height; when the host
+ is `height: auto` (the default on a flex item),
+ SimpleBar falls back to an "auto height" mode that
+ disables its internal scroll. Setting `height: 100%`
+ here gives the wrapper a resolvable percentage instead,
+ so SimpleBar drives scrolling against the flex-computed
+ height. SimpleBar also reads this element's `overflow-y`
+ and suppresses its own scrollbar if it's `hidden`, so
+ we use `overflow: hidden auto` to allow Y scrolling while
+ still blocking any horizontal overflow. */
+ height: 100%;
+ overflow: hidden auto;
+ margin: 2px 3px;
+ padding: 5px 0;
+ }
}
.popover-footer {
- text-align: center;
+ /* We center the attribution image with flex so it doesn't
+ pick up a baseline descender gap, and set flex-shrink: 0 so
+ the flex layout on `.popover-inner` can't squeeze the
+ footer below the image's own height. */
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ flex-shrink: 0;
background-color: hsl(0deg 0% 0%);
/* The border radius corresponds to the default radius value from `tippy-box`. */
border-radius: 0 0 4px 4px;
- /* This prevents the footer from experiencing height
- fluctuations at the moment when the image is uploaded. */
- min-height: 25px;
+ /* The GIPHY image is 200×42 natural, rendered at 120px wide
+ (so ~25px tall); 34px gives the image a little breathing
+ room and keeps the footer from collapsing before the image
+ has loaded. */
+ min-height: 34px;
& img {
width: 120px;