Skip to content

Commit

Permalink
Add a .click() event and padding parameter to the HTML component (
Browse files Browse the repository at this point in the history
#10155)

* add click event to html

* format

* add changeset

* add padding parameter

* fix test

* format

* add changeset

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
abidlabs and gradio-pr-bot authored Dec 11, 2024
1 parent 7d70596 commit 23a2958
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/public-owls-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/html": minor
"gradio": minor
---

feat:Add a `.click()` event and `padding` parameter to the `HTML` component
5 changes: 4 additions & 1 deletion gradio/components/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HTML(Component):
Guides: key-features
"""

EVENTS = [Events.change]
EVENTS = [Events.change, Events.click]

def __init__(
self,
Expand All @@ -41,6 +41,7 @@ def __init__(
min_height: int | None = None,
max_height: int | None = None,
container: bool = False,
padding: bool = True,
):
"""
Parameters:
Expand All @@ -57,9 +58,11 @@ def __init__(
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.
container: If True, the HTML component will be displayed in a container. Default is False.
padding: If True, the HTML component will have a certain padding (set by the `--block-padding` CSS variable) in all directions. Default is True.
"""
self.min_height = min_height
self.max_height = max_height
self.padding = padding
super().__init__(
label=label,
every=every,
Expand Down
6 changes: 5 additions & 1 deletion js/html/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
export let loading_status: LoadingStatus;
export let gradio: Gradio<{
change: never;
click: never;
clear_status: LoadingStatus;
}>;
export let show_label = false;
export let min_height: number | undefined = undefined;
export let max_height: number | undefined = undefined;
export let container = false;
export let padding = true;
$: label, gradio.dispatch("change");
</script>
Expand All @@ -39,6 +41,7 @@
/>
<div
class="html-container"
class:padding
class:pending={loading_status?.status === "pending"}
style:min-height={min_height && loading_status?.status !== "pending"
? css_units(min_height)
Expand All @@ -50,12 +53,13 @@
{elem_classes}
{visible}
on:change={() => gradio.dispatch("change")}
on:click={() => gradio.dispatch("click")}
/>
</div>
</Block>

<style>
.html-container {
.padding {
padding: var(--block-padding);
}
Expand Down
12 changes: 10 additions & 2 deletions js/html/shared/HTML.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
export let value: string;
export let visible = true;
const dispatch = createEventDispatcher<{ change: undefined }>();
const dispatch = createEventDispatcher<{
change: undefined;
click: undefined;
}>();
$: value, dispatch("change");
</script>

<div class="prose {elem_classes.join(' ')}" class:hide={!visible}>
<!-- svelte-ignore a11y-click-events-have-key-events a11y-no-static-element-interactions -->
<div
class="prose {elem_classes.join(' ')}"
class:hide={!visible}
on:click={() => dispatch("click")}
>
{@html value}
</div>

Expand Down
1 change: 1 addition & 0 deletions test/components/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_component_functions(self):
"min_height": None,
"max_height": None,
"container": False,
"padding": True,
}

def test_in_interface(self):
Expand Down

0 comments on commit 23a2958

Please sign in to comment.