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

Attach elem_classes selectors to layout elements, and an id to the Tab button (for targeting via CSS/JS) #5590

Merged
merged 16 commits into from
Sep 19, 2023

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Sep 18, 2023

This PR does two things:

  1. It passes the elem_classes attribute to the Blocks layouts (e.g. Row, Accordion, Tab, etc.) We had these attributes set up correctly in the frontend, but we were never passing them in from the frontend

  2. For the TabItem, the elem_id would get attached to the div holding the tab contents. We now also attach an {elem_id}-button to the button that you click on to select the tab. This closes: Add id, class, and or attribute to a Tab's button #5585

Test code:

import gradio as gr

css = """
#blue-button{
    background-color:aqua
}
#green-button {
    background-color:green
}
#red{
    background-color:red
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Tab(
            "Blue",
            elem_id="blue",
        ):
            gr.Textbox()
    with gr.Tab(
            "Green",
            elem_id="green",
        ):
            with gr.Accordion("Red background", elem_id="red"):
                gr.Textbox()
                gr.Textbox()
        
    demo.launch()

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Sep 18, 2023

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests all good! Build review
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/bae6a1ebcbd832f00c2b2b47cd22a28e5be89683/gradio-3.44.4-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@bae6a1ebcbd832f00c2b2b47cd22a28e5be89683#subdirectory=client/python"

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Sep 18, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/tabitem patch
@gradio/tabs patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Attach elem_classes selectors to layout elements, and an id to the Tab button (for targeting via CSS/JS)

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs added the v: patch A change that requires a patch release label Sep 18, 2023
@abidlabs abidlabs changed the title Attach elem_classes selectors to layout elements, including the TabItem Attach elem_classes selectors to layout elements, including the TabItem button Sep 18, 2023
@abidlabs abidlabs changed the title Attach elem_classes selectors to layout elements, including the TabItem button Attach elem_classes selectors to layout elements, and an id to the Tab button (for targeting via CSS/JS) Sep 18, 2023
<button class="selected">
<button
class="selected"
{...elem_id ? { id: t.elem_id + "-button" } : {}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Svelte if you set an attribute to null it gets removed. That might be a slightly cleaner way of expressing this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nice. Figured there'd a nicer way. Will update

@dawoodkhan82
Copy link
Collaborator

Nice! just tested and everything looks good.

Small nit: wondering if we should name the tab button {elem_id}-tab-button to be more specific.

@abidlabs
Copy link
Member Author

Thanks @dawoodkhan82 for reviewing!

I don't think we need to be more specific, users can select by tab-nav>{elem-id}-button if there's a different element with the {elem-id}-button ID somewhere on the page.

@abidlabs abidlabs merged commit d1ad1f6 into main Sep 19, 2023
16 checks passed
@abidlabs abidlabs deleted the tab-target branch September 19, 2023 17:34
This was referenced Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v: patch A change that requires a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add id, class, and or attribute to a Tab's button
4 participants