Skip to content

Commit

Permalink
Prevent HTML and Markdown height changing when status is hidden (#9363)
Browse files Browse the repository at this point in the history
* fix markdown height changing

* * add min_height param to html
* prevent height from changing based on status

* add changeset

* add changeset

* param desc change

* fix test

* format

* * add max height to html
* share css_units func

* add changeset

* fix backend test

* fe

---------

Co-authored-by: gradio-pr-bot <[email protected]>
Co-authored-by: Abubakar Abid <[email protected]>
  • Loading branch information
3 people authored Sep 19, 2024
1 parent 53ed0f0 commit 3ad28c7
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 24 deletions.
8 changes: 8 additions & 0 deletions .changeset/legal-masks-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@gradio/html": minor
"@gradio/markdown": minor
"@gradio/utils": minor
"gradio": minor
---

feat:Prevent HTML and Markdown height changing when status is hidden
6 changes: 6 additions & 0 deletions gradio/components/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def __init__(
elem_classes: list[str] | str | None = None,
render: bool = True,
key: int | str | None = None,
min_height: int | None = None,
max_height: int | None = None,
):
"""
Parameters:
Expand All @@ -51,7 +53,11 @@ def __init__(
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
key: if assigned, will be used to assume identity across a re-render. Components that have the same key across a re-render will have their value preserved.
min_height: The minimum height of the component, specified in pixels if a number is passed, or in CSS units if a string is passed. If HTML content exceeds the height, the component will expand to fit the content.
max_height: The maximum height of the component, specified in pixels if a number is passed, or in CSS units if a string is passed. If content exceeds the height, the component will scroll.
"""
self.min_height = min_height
self.max_height = max_height
super().__init__(
label=label,
every=every,
Expand Down
12 changes: 10 additions & 2 deletions js/html/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import type { LoadingStatus } from "@gradio/statustracker";
import { Block, BlockLabel } from "@gradio/atoms";
import { Code as CodeIcon } from "@gradio/icons";
import { css_units } from "@gradio/utils";
export let label: string;
export let elem_id = "";
Expand All @@ -17,6 +18,8 @@
clear_status: LoadingStatus;
}>;
export let show_label = false;
export let min_height: number | undefined = undefined;
export let max_height: number | undefined = undefined;
$: label, gradio.dispatch("change");
</script>
Expand All @@ -35,9 +38,14 @@
variant="center"
on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
/>
<div class:pending={loading_status?.status === "pending"}>
<div
class:pending={loading_status?.status === "pending"}
style:min-height={min_height && loading_status?.status !== "pending"
? css_units(min_height)
: undefined}
style:max-height={max_height ? css_units(max_height) : undefined}
>
<HTML
min_height={loading_status && loading_status?.status !== "complete"}
{value}
{elem_classes}
{visible}
Expand Down
11 changes: 2 additions & 9 deletions js/html/shared/HTML.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
<script lang="ts">
import { createEventDispatcher } from "svelte";
export let elem_classes: string[] = [];
export let value: string;
export let visible = true;
export let min_height = false;
const dispatch = createEventDispatcher<{ change: undefined }>();
$: value, dispatch("change");
</script>

<div
class="prose {elem_classes.join(' ')}"
class:min={min_height}
class:hide={!visible}
>
<div class="prose {elem_classes.join(' ')}" class:hide={!visible}>
{@html value}
</div>

<style>
.min {
min-height: var(--size-24);
}
.hide {
display: none;
}
Expand Down
2 changes: 1 addition & 1 deletion js/markdown/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
/>
<div class:pending={loading_status?.status === "pending"}>
<Markdown
min_height={loading_status && loading_status.status !== "complete"}
{value}
{elem_classes}
{visible}
Expand All @@ -72,6 +71,7 @@
{header_links}
{show_copy_button}
root={gradio.root}
{loading_status}
/>
</div>
</Block>
Expand Down
19 changes: 7 additions & 12 deletions js/markdown/shared/Markdown.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<script lang="ts">
import { createEventDispatcher } from "svelte";
import { copy } from "@gradio/utils";
import { copy, css_units } from "@gradio/utils";
import { Copy, Check } from "@gradio/icons";
import type { LoadingStatus } from "@gradio/statustracker";
import { IconButton, IconButtonWrapper } from "@gradio/atoms";
import MarkdownCode from "./MarkdownCode.svelte";
export let elem_classes: string[] = [];
export let visible = true;
export let value: string;
export let min_height = false;
export let min_height: number | string | undefined = undefined;
export let rtl = false;
export let sanitize_html = true;
export let line_breaks = false;
Expand All @@ -22,18 +23,13 @@
export let height: number | string | undefined = undefined;
export let show_copy_button = false;
export let root: string;
export let loading_status: LoadingStatus | undefined = undefined;
let copied = false;
let timer: NodeJS.Timeout;
const dispatch = createEventDispatcher<{ change: undefined }>();
const css_units = (dimension_value: string | number): string => {
return typeof dimension_value === "number"
? dimension_value + "px"
: dimension_value;
};
$: value, dispatch("change");
async function handle_copy(): Promise<void> {
Expand All @@ -53,13 +49,15 @@
</script>

<div
class:min={min_height}
class="prose {elem_classes.join(' ')}"
class:hide={!visible}
data-testid="markdown"
dir={rtl ? "rtl" : "ltr"}
use:copy
style={height ? `max-height: ${css_units(height)}; overflow-y: auto;` : ""}
style:min-height={min_height && loading_status?.status !== "pending"
? css_units(min_height)
: undefined}
>
{#if show_copy_button}
<IconButtonWrapper>
Expand Down Expand Up @@ -99,9 +97,6 @@
max-width: 100%;
}
.min {
min-height: var(--size-24);
}
.hide {
display: none;
}
Expand Down
6 changes: 6 additions & 0 deletions js/utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,9 @@ function _load_component(
variant
});
}

export const css_units = (dimension_value: string | number): string => {
return typeof dimension_value === "number"
? dimension_value + "px"
: dimension_value;
};
2 changes: 2 additions & 0 deletions test/components/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def test_component_functions(self):
"name": "html",
"_selectable": False,
"key": None,
"min_height": None,
"max_height": None,
}

def test_in_interface(self):
Expand Down

0 comments on commit 3ad28c7

Please sign in to comment.