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

Introduce getHtmlElement function #4123

Merged
merged 1 commit into from
Dec 18, 2024
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
2 changes: 1 addition & 1 deletion nicegui/elements/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, content: str = '', *, language: Optional[str] = 'python') ->
timer(0.1, self._update_copy_button)

self.client.on_connect(lambda: self.client.run_javascript(f'''
if (!navigator.clipboard) getElement({self.copy_button.id}).$el.style.display = 'none';
if (!navigator.clipboard) getHtmlElement({self.copy_button.id}).style.display = 'none';
'''))

async def show_checkmark(self) -> None:
Expand Down
3 changes: 2 additions & 1 deletion nicegui/functions/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def run_javascript(code: str, *, timeout: float = 1.0) -> AwaitableResponse:

This function runs arbitrary JavaScript code on a page that is executed in the browser.
The client must be connected before this function is called.
To access a client-side object by ID, use the JavaScript function `getElement()`.
To access a client-side Vue component or HTML element by ID,
use the JavaScript functions `getElement()` or `getHtmlElement()`.

If the function is awaited, the result of the JavaScript code is returned.
Otherwise, the JavaScript code is executed without waiting for a response.
Expand Down
2 changes: 1 addition & 1 deletion nicegui/page_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, *,
add_body_html(f'''
<script>
window.onload = () => {{
const header = getElement({self.id}).$el;
const header = getHtmlElement({self.id});
new ResizeObserver(() => {{
document.documentElement.style.scrollPaddingTop = `${{header.offsetHeight}}px`;
}}).observe(header);
Expand Down
4 changes: 4 additions & 0 deletions nicegui/static/nicegui.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ function getElement(id) {
return mounted_app.$refs["r" + _id];
}

function getHtmlElement(id) {
return document.getElementById(`c${id}`);
}

function runMethod(target, method_name, args) {
if (typeof target === "object") {
if (method_name in target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async def get_date():
ui.notify(f'Browser time: {time}')

def access_elements():
ui.run_javascript(f'getElement({label.id}).innerText += " Hello!"')
ui.run_javascript(f'getHtmlElement({label.id}).innerText += " Hello!"')

ui.button('fire and forget', on_click=alert)
ui.button('receive result', on_click=get_date)
Expand Down
Loading