Is there a way we can distinguish parent accordions from nested accordions in css without using ids? #1107
-
Hi A colleague and I are building a super awesome shiny app that contains several bslib accordions, some of which are nested within parent accordions. The code below represents a dummy app I've created to demonstrate the intended color system. Accordion 3 has three nested accordions within it. I have three questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can use the CSS selector /* Nested accordions */
/* Indent titles of nested accordions */
.accordion .accordion .accordion-button {
padding-inline-start: 2rem;
}
/* Accordions should have a maroon background with white text when collapsed*/
.accordion .accordion .accordion-button.collapsed {
color: white;
background-color: maroon;
}
/* Accordions should have white background with maroon text when not collapsed*/
.accordion .accordion .accordion-button:not(.collapsed) {
color: maroon;
background-color: white;
} Also, you can use some Bootstrap utility classes to improve the appearance of the nested accordions. In the third Here's an example app on shinylive.io. Example applibrary(shiny)
library(bslib)
## In this dummy app, we have three accordions. Accordion 3 consists of nested accordions.
## We are trying to figure out a way to style the accordions differently, depending on whether they are nested or
## not.
## Please see the css code included in app_ui
mod_acc_UI <- function(id) {
ns <- NS(id)
tagList(
accordion(
open = "accordion_3",
id = ns("main_accordion"),
## Accordion 1
accordion_panel(
title = "Accordion 1",
value = "accordion_1"
),
## Accordion 2
accordion_panel(
title = "Accordion 2",
value = "accordion_2"
),
## Accordion 3 is a parent accordion that contains 3 nested accordions
accordion_panel(
title = "Accordion 3",
value = "accordion_3",
class = "p-0",
accordion(
id = ns("accordion3_with_nested_accordions"),
class = "accordion-flush",
open = FALSE,
accordion_panel(
title = "Nested accordion 1",
value = "nested_accordion_1"
),
accordion_panel(
title = "Nested accordion 2",
value = "nested_accordion_2"
),
accordion_panel(
title = "Nested accordion 3",
value = "nested_accordion_3"
)
)
)
)
)
}
mod_acc_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
ns <- session$ns
}
)
}
app_ui <- fluidPage(
tags$head(
tags$style(
HTML("
/* Parent accordions */
/* Accordions should have a green background with white text when collapsed*/
.accordion-button.collapsed {
color: white;
background-color: green;
}
/*Accordions should have white background with green text when not collapsed*/
.accordion-button:not(.collapsed) {
color: green;
background-color: white;
}
/* Nested accordions */
/* Indent titles of nested accordions */
.accordion .accordion .accordion-button {
padding-inline-start: 2rem;
}
/* Accordions should have a maroon background with white text when collapsed*/
.accordion .accordion .accordion-button.collapsed {
color: white;
background-color: maroon;
}
/* Accordions should have white background with maroon text when not collapsed*/
.accordion .accordion .accordion-button:not(.collapsed) {
color: maroon;
background-color: white;
}
")
)
),
theme = bs_theme(version = 5),
mod_acc_UI("test")
)
app_server <- function(input, output, session) {
mod_acc_Server("test")
}
shinyApp(
ui = app_ui,
server = app_server
) |
Beta Was this translation helpful? Give feedback.
-
Hi @gadenbuie , Thank you so much. Your solution works perfectly. I'll use the same piece of code to ask a follow up question (#1109). |
Beta Was this translation helpful? Give feedback.
You can use the CSS selector
.accordion .accordion
to target an accordion nested within another accordion. So you can use rules like this to get the appearance you'd like:Also, you can use som…