Skip to content

Implement a script tag #544

Closed
Closed
@Archmonger

Description

@Archmonger

Current Situation

There are several situations where the user might want to run simple JavaScript on-demand.

For example, in Django-IDOM when a sign out button is clicked, the user is signed out from the backend but there's no way to window.location.reload or set the current window.location = ....

Or, if using non-react libraries (ex. classic bootstrap), it would be convenient to be able to run $("#modal").toggle().

Proposed Changes

Allow components to have some way of running some arbitrary JavaScript.

Option 1: Script Parameter

idom.html.button("click me", script="window.location.reload()")

Option 2: Script Executor

# This feels like the most pythonic to exec a script
idom.exec_script("window.location.reload()");

Option 3: Script Tag

# This feels like the most IDOM-like method of script execution
# Should be executed if the internal contents of the `script` tag has changed.
idom.html.div(
    idom.html.script("window.location.reload()")
)

Option 4: Script Hook

@hooks.use_script
async def my_javascript(context):
    return "window.location.reload()"

Implementation Details

Will need to be careful to not eval this script on every component re-render.

See more details here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority-2-moderateShould be resolved on a reasonable timeline.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions