WannabeSmith
WannabeSmith

Reputation: 445

header style not working when putting a button in the header of shinydashboard

I managed to put an action button in the shiny dashboard's header. However, when applying styling using tags$li, it only applies to the sidebar. When removing the tags$a portion, the styling gets applied throughout the header. Not sure how to fix it, so the styling is consistent across the header- was hoping to get some hint/directions in stack overflow.

I have seen these posts:

  1. Home Button in Header in R shiny Dashboard
  2. Login Button in shinydashboard dashboardHeader

This question is extension of my previous question: resizing an action button causes misalignment in the header in shinydashboard

Here is a reprex (with an image below):

library(shiny)
library(shinydashboard)
library(htmltools)
library(shinyjs)
ui <- dashboardPage(
  dashboardHeader(
    
    tags$li(class = "dropdown",
                    tags$a(actionButton(inputId = "email1", label = "",
                                        icon = icon("envelope", lib = "font-awesome")
                                        
                                        #Also tried to adjust the width and height of transparent, no luck 
                                        # ,
                                        # style='height: 20px;
                                        #       background: transparent;
                                        #       border: none;
                                        #       font-size: 2rem;
                                        #       transform: translate(5%, -30%);'

                    ),
                    href="mailto:[email protected];"),
                    
                    # has no effect on the main header bar (the header in which button is placed)
                    tags$style(".main-header {max-height: 20px}"),
                    tags$style(".main-header .logo {height: 20px;}"),
                    tags$style(".sidebar-toggle {height: 20px; padding-top: 1px !important;}"),
                    tags$style(".navbar {min-height:20px !important}")
            )
  ),
  dashboardSidebar(
  ),
  dashboardBody()
)

server <- function(input, output){}

shinyApp(ui, server)

Thank you very much for your help!

enter image description here

Upvotes: 0

Views: 337

Answers (2)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

That's because of the paddings of the button and the a tag. You can do:

        tags$a(actionButton(inputId = "email1", label = "",
                            icon = icon("envelope", lib = "font-awesome"),
                            style = "padding-top: 0; padding-bottom: 0;"),
        href="mailto:[email protected];", 
        style = "padding-top: 0; padding-bottom: 0;")

If you don't use the action button, it is better to do:

library(fontawesome)

and

tags$li(class = "dropdown",
        tags$a(fa("envelope"),
        href="mailto:[email protected];", 
        style = "padding-top: 0; padding-bottom: 0;"),

Upvotes: 1

YBS
YBS

Reputation: 21297

Your code works fine if you don't have an email icon in the header. Try this

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(
    
    # tags$li(class = "dropdown",
    #         tags$a(actionButton(inputId = "email1", label = "",
    #                             icon = icon("envelope", lib = "font-awesome")
    # 
    #                             #Also tried to adjust the width and height of transparent, no luck
    #                             # ,
    #                             # style='height: 20px;
    #                             #       background: transparent;
    #                             #       border: none;
    #                             #       font-size: 2rem;
    #                             #       transform: translate(5%, -30%);'
    # 
    #         ),
    #         href="mailto:[email protected];"),
    # ),
    tags$li(class = "dropdown",
            # has effect on the main header bar
            tags$style(".main-header {max-height: 20px !important;}"),
            tags$style(".main-header .logo {height: 20px !important;}"),
            tags$style(".sidebar-toggle {height: 20px; padding-top: 1px !important;}"),
            tags$style(".navbar {min-height:20px !important}")
    )
    
  ),
  dashboardSidebar(
    # Adjust the sidebar
    tags$style(".left-side, .main-sidebar {padding-top: 20px}")
  ),
  dashboardBody()
)

server <- function(input, output){}

shinyApp(ui, server)

output

Upvotes: 1

Related Questions