Skip to content

Commit

Permalink
Textbox and Code Component Blur/Focus Fixes (#6323)
Browse files Browse the repository at this point in the history
* focus and blur events

* format

* add changeset

* fixes

* fixes

* format

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
dawoodkhan82 and gradio-pr-bot committed Nov 10, 2023
1 parent 854b482 commit 55fda81
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/ready-dancers-camp.md
@@ -0,0 +1,7 @@
---
"@gradio/code": patch
"@gradio/textbox": patch
"gradio": patch
---

fix:Textbox and Code Component Blur/Focus Fixes
7 changes: 6 additions & 1 deletion gradio/components/code.py
Expand Up @@ -36,7 +36,12 @@ class Code(Component):
None,
]

EVENTS = [Events.change, Events.input]
EVENTS = [
Events.change,
Events.input,
Events.focus,
Events.blur,
]

def __init__(
self,
Expand Down
12 changes: 11 additions & 1 deletion js/code/Index.svelte
Expand Up @@ -21,6 +21,8 @@
export let gradio: Gradio<{
change: typeof value;
input: never;
blur: never;
focus: never;
}>;
export let value = "";
export let value_is_output = false;
Expand Down Expand Up @@ -74,6 +76,14 @@
{:else}
<Widget {language} {value} />

<Code bind:value {language} {lines} {dark_mode} readonly={!interactive} />
<Code
bind:value
{language}
{lines}
{dark_mode}
readonly={!interactive}
on:blur={() => gradio.dispatch("blur")}
on:focus={() => gradio.dispatch("focus")}
/>
{/if}
</Block>
19 changes: 17 additions & 2 deletions js/code/shared/Code.svelte
Expand Up @@ -28,7 +28,11 @@
export let readonly = false;
export let placeholder: string | HTMLElement | null | undefined = undefined;
const dispatch = createEventDispatcher<{ change: string }>();
const dispatch = createEventDispatcher<{
change: string;
blur: undefined;
focus: undefined;
}>();
let lang_extension: Extension | undefined;
let element: HTMLDivElement;
let view: EditorView;
Expand Down Expand Up @@ -63,10 +67,21 @@
}
function createEditorView(): EditorView {
return new EditorView({
const editorView = new EditorView({
parent: element,
state: createEditorState(value)
});
editorView.dom.addEventListener("focus", handleFocus, true);
editorView.dom.addEventListener("blur", handleBlur, true);
return editorView;
}
function handleFocus(): void {
dispatch("focus");
}
function handleBlur(): void {
dispatch("blur");
}
function getGutterLineHeight(_view: EditorView): string | null {
Expand Down
3 changes: 3 additions & 0 deletions js/textbox/shared/Textbox.svelte
Expand Up @@ -64,6 +64,9 @@
}
}
afterUpdate(() => {
if (autofocus) {
el.focus();
}
if (can_scroll && autoscroll) {
scroll();
}
Expand Down

0 comments on commit 55fda81

Please sign in to comment.