Contribute to the DSpace Development Fund

The newly established DSpace Development Fund supports the development of new features prioritized by DSpace Governance. For a list of planned features see the fund wiki page.

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 15 Next »

Angular Overview

The DSpace User Interface (UI) is built on the Angular.io framework.  All data comes from the REST API (DSpace Backend), and the final HTML pages are generated via TypeScript.

Before getting started in customizing or branding the UI, there are some basic Angular concepts to be aware of.  You do not need to know Angular or TypeScript to theme or brand the UI. But, understanding a few basic concepts will allow you to better understand the folder structure / layout of the codebase.

Angular Components: In Angular, every webpage consists of a number of "components" which define the structure of a page.  They are the main "building block" of any Angular application. Components are reusable across many pages. So, for example, there's only one "header" and "footer" component, even though they appear across all pages.

Each Component has:

  • A *.component.ts (TypeScript) file which contains the logic & name ("selector") of the component
  • A *.component.html (HTML) file which contains the HTML markup for the component (and possibly references to other embedded components).  This is also called the "template".
    • In HTML files, components are named/referenced as HTML-like tags (e.g. <ds-header>, <ds-footer>). In DSpace's UI, every component starts with "ds-" in order to distinguish it as an out-of-the-box DSpace component. 
  • A *.component.scss (Sass / CSS) file which contains the style for the component.

If you want a deeper dive into Angular concepts of Components and Templates, see https://angular.io/guide/architecture-components

Theme Technologies

The DSpace UI uses:

  • Bootstrap (v4.x) website framework for general layout & webpage components (buttons, alerts, etc)
  • Sass, a CSS preprocessor, for stylesheets. Sass is very similar to CSS (an in fact, any CSS is valid Sass). But, Sass allows you to nest CSS rules & have variables and functions.  For a brief overview on Sass, see https://sass-lang.com/guide
  • HTML5, the latest specification of the HTML language

Familiarity with these technologies (or even just CSS + HTML) is all you need to do basic theming of the DSpace UI.

Creating a Custom Theme

Theme Directories & Design Principles

Out of the box, there are three theming layers/directories to be aware of:

  • Base Theme (src/app/ directories):  The primary look & feel of DSpace (e.g. HTML layout, header/footer, etc) is defined by the HTML5 templates under this directory. Each HTML5 template is stored in a subdirectory named for the Angular component where that template is used. The base theme includes very limited styling (CSS, etc), based heavily on default Bootstrap (4.x) styling, and only allowing for minor tweaks to improve WCAG 2.1 AA accessibility.
  • Custom Theme (src/themes/custom directories): This directory acts as the scaffolding or template for creating a new custom theme.  It provides (empty) Angular components/templates which allow you to change the theme of individual components.  Since all files are empty by default, if you enable this theme (without modifying it), it will look identical to the Base Theme.
  • DSpace Theme (src/themes/dspace directories): This is the default theme for DSpace 7.  It's a very simple example theme providing a custom color scheme & homepage on top of the Base Theme. It's important to note that this theme ONLY provides custom CSS/images to override our Base Theme. All HTML5 templates are included at the Base Theme level, as this ensures those HTML5 templates are also available to the Custom Theme.

The DSpace UI design principles & technologies are described in more detail at DSpace UI Design principles and guidelines

Getting Started

  1. Start with the "custom" theme: The best place to start with a new theme is the "custom" theme folder (src/themes/custom). This folder contains the boilerplate code for all theme-able components. It's a scaffolding for a new theme which doesn't modify any of the "base theme" (src/app/ directories). This means that by default it's a plain Bootstrap look and feel, with a few tweaks for better accessibility.
  2. Create your own theme folder OR edit the "custom" theme: Either edit the "custom" theme directory, or copy the "custom" theme folder (and all its contents) into a new folder under src/themes/ (choose whatever folder name you want)
  3. Register your theme folder (only necessary if you create a new folder in previous step): Now, we need to make the UI aware of this new theme folder, before it can be used in configuration.
    1. Modify angular.json (in the root folder), adding your theme folder's main "theme.scss" file to the "styles" list.  The below example is for a new theme folder named src/themes/mydspacesite/

      "styles": [
        "src/styles/startup.scss",
        {
           "input": "src/styles/base-theme.scss",
           "inject": false,
           "bundleName": "base-theme"
        },
        ...
        {
           "input": "src/themes/mydspacesite/styles/theme.scss",
           "inject": false,
           "bundleName": "mydspacesite-theme"
        },
      ]

      NOTE: the "bundleName" for your custom them MUST use the format "${folder-name}-theme".  E.g. if the folder is named "arc/themes/amazingtheme", then the "bundleName" MUST be "amazingtheme-theme"

  4. Enable your theme: Modify your src/environments/environment.*.ts configuration file, adding your new theme to the "themes" array in that file.  Pay close attention to modify the correct environment file (e.g. modify environment.dev.ts if running in dev mode, or environment.prod.ts if running in prod mode).  We recommend starting in "dev mode" (environment.dev.ts) as this mode lets you see your changes immediately in a browser without a full rebuild of the UI – see next step. 

    // In this example, we only show one theme enabled. It's possible to enable multiple (see below note)
    themes: [
     {
        name: 'mydspacesite'
     },
    ]
    NOTE: The "name" used is the name of the theme's folder, so the example is for enabling a theme at src/themes/mydspacesite/ globally.  You should also comment out the default "dspace" theme, if you intend to replace it entirely.
    NOTE #2: You may also choose to enable multiple themes for your site, and even specify a different theme for different Communities, Collections, Items or URL paths. See User Interface Configuration for more details on "Theme Settings" in your environment.*.ts
  5. Verify your settings by starting the UI (ideally in Dev mode): At this point, you should verify the basic settings you've made all "work".  We recommend doing your theme work while running the UI in "dev mode", as the UI will auto-restart anytime you save a new change.  This will allow you to quickly see the impact of each change in your browser. 

    # Start in dev mode (which uses environment.dev.ts)
    yarn start:dev
  6. At this point, you can start making changes to your theme.  See the next two sections for examples of how to make common changes.

Global style/font/color customizations

Changes to the global Bootstrap variables or styles will apply to all pages / Angular components across the entire site.

  1. Global style changes: All global style changes can be made in your theme's styles folder (e.g. src/themes/mydspacesite/styles). There are four main files in that folder:
    1. _theme_sass_variable_overrides.scss - May be used to override Bootstrap's default Sass variables.  This is the file you may wish to use for most  style changes. There are a large number of Bootstrap variables available which control everything from fonts, colors and the base style for all Bootstrap web components.  For a full list of Bootstrap variables you can override in this file, see the node_modules/bootstrap/scss/_variables.scss file (which is installed in your source directory when you run "yarn install"). More information may also be found in the Bootstrap Sass documentation at https://getbootstrap.com/docs/4.0/getting-started/theming/#sass
    2. _theme_css_variable_overrides.scss - May be used to override DSpace's default CSS variables. DSpace's UI uses CSS variables for all its components. These variables all start with "--ds-*", and are listed in src/styles/_custom_variables.scss. You can also use this file to add your own, custom CSS variables which you want to use for your theme.  If you create custom variables, avoid naming them with a "--ds-*" or a "--bs-*" prefix, as those are reserved for DSpace and Bootstrap variables.
    3. _global-styles.scss - May be used to modify the global CSS/SCSS for the site.  This file may be used to override the default global style contained in src/styles/_global-styles.scss .  Keep in mind, many styles can be more quickly changed by simply updating a variable in one of the above "*_variable_overrides.scss" files.  So, it's often easier to use those first, where possible.
    4. theme.scss - This just imports all the necessary Sass files to create the theme. It's unnecessary to modify directly, unless you with to add new Sass files to your theme.
  2. Modifying the default font:  By default, DSpace uses Bootstrap's "native font stack", which just uses system UI fonts. However, the font used in your site can be quickly updated via Bootstrap variables in your theme's _theme_sass_variable_overrides.scss file. 
    1. One option is to add a new import statement and modify the "$font-family-sans-serif" variable:

      // Import the font (from a URL)
      @import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro');
      
      // Configure Bootstrap to use this font (and list a number of backup fonts to use on various systems)
      $font-family-sans-serif: 'Source Sans Pro', -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol" !default;
    2. If your font requires installing local files, you can do the following
      1. Copy your font file(s) in your theme's assets/fonts/ folder
      2. Create a new ".scss" file specific to your font in that folder, e.g. assets/fonts/open-sans.scss, and use the "@font-face" CSS rule to load that font:

        open-sans.scss
        @font-face {
          font-family: "Open Sans";
          src: url("/assets/fonts/OpenSans-Regular-webfont.woff2") format("woff2"),
               url("/assets/fonts/OpenSans-Regular-webfont.woff") format("woff");
        }
      3. Then, import that new "open-sans.scss" file and use it in the "$font-family-sans-serif" variable

        // Import the font via the custom SCSS file
        @import '../assets/fonts/open-sans';
        
        // Configure Bootstrap to use this font (and list a number of backup fonts to use on various systems) 
        $font-family-sans-serif: 'Open Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol" !default;
    3. Keep in mind, as changing the font just requires adjusting Bootstrap Sass variables, there are a lot of Bootstrap guides out there that can help you make more advanced changes
  3. Modifying default color scheme: The colors used in your site can be quickly updated via Bootstrap variables in your theme's _theme_sass_variable_overrides.scss file. 
    1. Again, you can use entirely Bootstrap variables to adjust color schemes.  See the Bootstrap Theming Colors documentation
    2. A list of all Bootstrap color variables can be found in the node_modules/bootstrap/scss/_variables.scss file
    3. Additional examples can be found in the out-of-the-box "dspace" theme, which adjusts the default Bootstrap colors slightly for both accessibility & to match the DSpace logo.

Modify Logo in Header

  1. Copy your logo to your theme's assets/image/ folder
  2. Edit your theme's app/header/header.components.ts file & swap the "templateUrl" properties

    header.component.ts
    @Component({
      selector: 'ds-header',
      // styleUrls: ['header.component.scss'],
      styleUrls: ['../../../../app/header/header.component.scss'],
      // Uncomment the templateUrl which references the "header.component.html" file in your theme directory
      templateUrl: 'header.component.html',
      // Comment out the templateUrl which references the default "src/app/header/header.component.html" file.
      //templateUrl: '../../../../app/header/header.component.html',
    })
  3. Your theme's version of the header.component.html file will be empty by default. Copy over the default HTML code from src/app/header/header.component.html into your version of this file.
  4. Then, modify your copy of header.component.html to use your logo. In this example, we're assuming you have a logo file named assets/images/my-logo.svg:

    <header>
      <div class="container">
        <div class="d-flex flex-row justify-content-between">
          <a class="navbar-brand my-2" routerLink="/home">
            <!-- Modify the logo on the next line -->
            <img src="assets/images/my-logo.svg" [attr.alt]="'menu.header.image.logo' | translate"/>
          </a>
          ...
    </header>
  5. Obviously, you can also make additional modifications to the HTML of the header in this file!  You'll also see that the header references several other DSpace UI components (e.g. <ds-search-navbar> is the search icon in the header). You can easily comment out these tags to disable them, or move them around to change where that component appears in the header.


Additional Theming Resources

  • No labels