17.3.12 • Published 3 months ago

ng-alt-sidebar v17.3.12

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

ng-alt-sidebar

An Angular alternative sidebar component.

Formerly called ng-sidebar

⚠️ This package is forked form deprecated package ng-sidebar . ⚠️


Build Status NPM Version NPM Downloads

Example

https://megamanhxh.github.io/ng-alt-sidebar/

Installation

npm install --save ng-alt-sidebar

SystemJS configuration

Note: If you were using SystemJS, huge chance it will not work anymore due to the change of module exporting.

Changelog

See the releases page on GitHub.

Usage

Add AltSidebarModule to your app module:

import { AltSidebarModule } from 'ng-alt-sidebar';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, AltSidebarModule.forRoot()],
  bootstrap: [AppComponent],
})
class AppModule {}

In your app component, simply use add a <ng-alt-sidebar-container> wrapper, then place your <ng-alt-sidebar>(s) and content within it. Your page content should be in some container with a ng-alt-sidebar-content attribute.

@Component({
  selector: 'app',
  template: `
    <!-- Container for sidebar(s) + page content -->
    <ng-alt-sidebar-container>

      <!-- A sidebar -->
      <ng-alt-sidebar [(opened)]="_opened">
        <p>Sidebar contents</p>
      </ng-alt-sidebar>

      <!-- Page content -->
      <div ng-alt-sidebar-content>
        <button (click)="_toggleSidebar()">Toggle sidebar</button>
      </div>

    </ng-alt-sidebar-container>
  `
})
class AppComponent {
  private _opened: boolean = false;

  private _toggleSidebar() {
    this._opened = !this._opened;
  }
}

If nothing seems to show up, your wrappers' heights may be collapsing. Try adding a height (e.g. height: 100vh;) to the wrapper <ng-alt-sidebar-container> or other wrapper elements you may have. (See issue #100 for more info.)

A directive is also provided to easily close the sidebar by clicking something inside it:

<ng-alt-sidebar>
  <a closeSidebar>Closes the sidebar</a>
</ng-alt-sidebar>

You can also use the open() and close() functions:

<ng-alt-sidebar #sidebar>
    <button (click)="sidebar.close()">Close sidebar</button>
</ng-alt-sidebar>

<button (click)="sidebar.open()">Open sidebar</button>

Functions

The sidebar has a few public functions:

FunctionDescription
open()Opens the sidebar.
close()Closes the sidebar.
triggerRerender()Manually trigger a re-render of the container. Useful if the sidebar contents might change.

Styling

Various class names are attached to the sidebar and container for easier styling.

If you are using Angular's default emulated view encapsulation, you may have to use the >>> selector to target the sidebar's classes. Check out Angular's documentation for more details. Note that the /deep/ selector will soon be deprecated.

Sidebar

Class nameDescription
ng-alt-sidebarAlways on the sidebar element.
ng-alt-sidebar--openedWhen opened is true.
ng-alt-sidebar--closedWhen opened is false.
ng-alt-sidebar--leftWhen position is 'left' (or the 'start'/'end' aliases are equivalent to 'left').
ng-alt-sidebar--rightWhen position is 'right' (or the 'start'/'end' aliases are equivalent to 'right').
ng-alt-sidebar--topWhen position is 'top'.
ng-alt-sidebar--bottomWhen position is 'bottom'.
ng-alt-sidebar--overWhen mode is 'over'.
ng-alt-sidebar--pushWhen mode is 'push'.
ng-alt-sidebar--slideWhen mode is 'slide'.
ng-alt-sidebar--dockedWhen the sidebar is docked (i.e. it is "closed" and dock is true).
ng-alt-sidebar--inertIgnores pointer clicks. Class is applied when the sidebar is closed.
ng-alt-sidebar--animateWhen animate is true for a sidebar.

Backdrop

Class nameDescription
ng-alt-sidebar__backdropClass of the backdrop div. Note that the div is only in the DOM when the backdrop is shown.

Page content

Class nameDescription
ng-alt-sidebar__contentClass of the wrapper div for the page content.
ng-alt-sidebar__content--animateWhen animate is true for the container.

Options

<ng-alt-sidebar-container>

Inputs

Property nameTypeDefaultDescription
contentClassstringAdditional class name on the div wrapping the page contents.
backdropClassstringAdditional class name on the overlay element.
showBackdropbooleanfalseControls the backdrop state of the sidebar container. This should be two-way bound.
allowSidebarBackdropControlbooleantrueDetermines if the container component respects the sidebar's showBackdrop input option.
animatebooleantrueAnimates the container sliding.

Outputs

Property nameCallback argumentsDescription
showBackdropChangeshowBackdrop: booleanEmitted when showBackdrop is modified. This allows for you to do "two-way binding" (i.e. [(showBackdrop)]).
onBackdropClickedEmitted when a backdrop is clicked.

<ng-alt-sidebar>

Inputs

Property nameTypeDefaultDescription
openedbooleanfalseControls the opened state of the sidebar. This should be two-way bound.
mode'over', 'push', 'slide''over'See the "Modes" section.
dockbooleanfalseShow the sidebar as docked when closed.
dockedSizestring'0px'When mode is set to 'dock', this value indicates how much of the sidebar is still visible when "closed".
position'left', 'right', 'top', 'bottom', 'start', 'end''start'What side the sidebar should be docked to. 'start' and 'end' are aliases that respect the page's language (e.g. start is the same as left for English, but would be right for Hebrew.
autoCollapseHeightnumberWindow height in pixels in which to automatically close the sidebar.
autoCollapseWidthnumberWindow width in pixels in which to automatically close the sidebar.
autoCollapseOnInitbooleantrueCollapse sidebar based on autoCollapseHeight and/or autoCollapseWidth on initial render as needed.
animatebooleantrueAnimate the opening/closing of the sidebar.
sidebarClassstringAdditional class name on the sidebar element.
ariaLabelstringValue for the sidebar's aria-label attribute.
trapFocusbooleanfalseKeeps focus within the sidebar when open. Note that this only works if there's one sidebar open at a time.
autoFocusbooleantrueAutomatically focus the first focusable element in the sidebar when opened.
showBackdropbooleanfalseIf a translucent black backdrop overlay should appear over the page contents when the sidebar is opened. This is ignored if the sidebar's parent container has its allowSidebarBackdropControl property set to true.
closeOnClickBackdropbooleanfalseWhether clicking on the backdrop of the open sidebar will close it.
closeOnClickOutsidebooleanfalseWhether clicking outside of the open sidebar will close it.
keyClosebooleanfalseClose the sidebar when a keyboard button is pressed.
keyCodenumber27The key code for keyClose.

Outputs

Property nameCallback argumentsDescription
onContentInitCorresponds with ngAfterContentInit lifecycle event of the sidebar component.
openedChangeopened: booleanEmitted when opened is modified. This allows for you to do "two-way binding" (i.e. [(opened)]).
onOpenStartEmitted when the sidebar is opening.
onOpenedEmitted when the sidebar is opened.
onCloseStartEmitted when the sidebar is closing.
onClosedEmitted when the sidebar is closed.
onTransitionEndEmitted when the animation for opening or closing ends.
onModeChangemode: stringEmitted when mode is changed.
onPositionChangeposition: stringEmitted when position is changed.

Modes

over

This is the default mode. The sidebar slides in over the page contents.

push

The page contents is pushed to make space for the sidebar.

slide

The entire page slides over to show the sidebar. Note that this only works if you have one sidebar open at a time.