---
title: "Theme for shiny"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Theme for shiny}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)
```

```{r setup}
library(fresh)
```


{fresh} allow to customize themes to use with `shiny::fluidPage` or `shiny::navbarPage`. [{shiny}](https://github.com/rstudio/shiny) use by default the theme from [Bootstrap](https://getbootstrap.com/docs/3.4/), [{shinythemes}](https://github.com/rstudio/shinythemes) allow to use custom themes from [Bootswatch](https://bootswatch.com/3/). All these themes have default parameters (like colors) that {fresh} allow to edit.


## Create a theme

Use function `create_theme` to create a new theme, first argument is the default template you want to modify, after that you can use functions `bs_vars_*` to edit variables of Bootstrap elements :


```{r}
create_theme(
  theme = "default",
  bs_vars_button(
    default_color = "#FFF",
    default_bg = "#112446",
    default_border = "#112446",
    border_radius_base = "15px"
  ),
  bs_vars_wells(
    bg = "#FFF",
    border = "#112446"
  )
)
```

Here we modify `shiny::actionButton` appearance with `bs_vars_button` and appearance of `shiny::sidebarPanel` with `bs_vars_wells`.


Result looks like :

<!-- ![](figures/example_shiny.png) -->

```{r, eval=TRUE, echo=FALSE}
knitr::include_graphics(path = "figures/example_shiny.png")
```




## Use a theme

There's two way to use a newly created theme : 

1. inline in the UI (useful to quickly visualize your theme)
2. using a CSS file (recommended)


### Inline

Don't specify an output file and use rsult of `create_theme()` in `use_theme()` directly in your application : 

```{r}
navbarPage(
  title = "Custom navbar",
  header = use_theme(
    create_theme(
      theme = "default",
      bs_vars_navbar(
        default_bg = "#75b8d1",
        default_color = "#FFFFFF",
        default_link_color = "#FFFFFF",
        default_link_active_color = "#75b8d1",
        default_link_active_bg = "#FFFFFF",
        default_link_hover_color = "firebrick"
      ),
      output_file = NULL
    )
  ),
  tabPanel("Tab 1"),
  tabPanel("Tab 2")
)
```



### File

When you create a theme, you can specify an output file : 

```{r}
create_theme(
  theme = "default", 
  ...,
  output_file = "my-custom-theme.css"
)
```

Put the file `my-custom-theme.css` in the `www/` folder of your application, and reference it like that in your UI : 

```r
fluidPage(
  
  theme = "my-custom-theme.css",
  
  # ...
)

# or if using a navbar page
navbarPage(
  
  title = "My application",
  theme = "mytheme.css",
  
  # ...
)
```




