Skip to content

Allow setting attributes at the page level, especiallyclass and style #899

Open

Description

@gadenbuie

It's currently difficult to add page-level attributes, and our page functions provide an inconsistent API. Supposing I'd like to add the foo class to the <body> element, only page() and page_fillable() give the desired results.

Function Call Result
page(class = "foo") .foo is added to the <body>
page_fillable(class = "foo") .foo is added to the <body>
page_navbar(class = "foo") Errors
page_sidebar(class = "foo") .foo is added to the .main content area of the sidebar layout (passes class to layout_sidebar())
page_fluid(class = "foo") Returns a div.container-fluid.foo, not a body tag
page_fixed(class = "foo") Returns a div.container.foo element, not a body tag

There are two key challenges in resolving this problem:

  1. We can upgrade page_fluid() and page_fixed() to use bslib::page() but for backwards compatibility, the ... elements should be handed to div(class = "container, ...).
  2. We can't redirect the ... in page_sidebar() and page_navbar() without breaking behavior since they're used by layout_sidebar() and the navset generator function.

Proposal

We add an argument that collects page-level attributes to each of the above functions, named something like page_attrs or .attributes. This argument would take a list – created with list() or a helper like page_attributes() – and would apply the attributes to the page element.

In page_fillable() and page(), where the ... are already passed to the tags$body(), we'd fold this list into that call. In page_fluid() and page_fixed() we'd pass the page_attrs to the page() function and continue to pass the ... to the container div.

page(page_attrs = list(class = "foo"))
page_fillable(page_attrs = list(class = "foo"))
page_navbar(page_attrs = list(class = "foo"))
page_sidebar(page_attrs = list(class = "foo"))
page_fluid(page_attrs = list(class = "foo"))
page_fixed(page_attrs = list(class = "foo"))

A more automagical approach would be to allow page_attributes() to be included unnamed in the ... of the above functions. The page functions could inspect the ..., pull out the page attributes object and apply the attributes appropriately to the body tag.

page(page_attributes(class = "foo"))
page_fillable(page_attributes(class = "foo"))
page_navbar(page_attributes(class = "foo"))
page_sidebar(page_attributes(class = "foo"))
page_fluid(page_attributes(class = "foo"))
page_fixed(page_attributes(class = "foo"))

Activity

  1. cpsievert commented on Nov 8, 2023

    @cpsievert
    Collaborator

    Oh interesting. I kind of like the idea of page_attributes(), which could be implemented as a web components that add/removes attributes on the document.body during connectedCallback()/disconnectedCallback() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions