2024-07-17 15:23:16 -04:00
---
meta:
title: Angular
2024-12-06 14:27:35 -05:00
description: Tips for using Nebula in your Angular app.
2024-07-17 15:23:16 -04:00
---
# Angular
2024-12-06 14:27:35 -05:00
Angular [plays nice ](https://custom-elements-everywhere.com/#angular ) with custom elements, so you can use Nebula in your Angular apps with ease.
2024-07-17 15:23:16 -04:00
## Installation
### Download the npm package
2024-12-06 14:27:35 -05:00
To add Nebula to your Angular app, install the package from npm.
2024-07-17 15:23:16 -04:00
```bash
2024-12-06 14:27:35 -05:00
npm install @onsonr/nebula
2024-07-17 15:23:16 -04:00
```
### Update the Angular Configuration
Next, [include a theme ](/getting-started/themes ). In this example, we'll import the light theme.
Its also important to load the components by using a `<script>` tag into the index.html file. However, the Angular way to do it is by adding a script configurations into your angular.json file as follows:
```json
"architect": {
"build": {
...
"options": {
...
"styles": [
"src/styles.scss",
2024-12-06 14:27:35 -05:00
"@onsonr/nebula/dist/themes/light .css"
2024-07-17 15:23:16 -04:00
],
"scripts": [
2024-12-06 14:27:35 -05:00
"@onsonr/nebula/dist/shoelace .js"
2024-07-17 15:23:16 -04:00
]
...
```
### Setting up the base path
Next, set the [base path ](/getting-started/installation#setting-the-base-path ) for icons and other assets in the `main.ts` . In this example, we'll use the CDN as a base path.
```jsx
2024-12-06 14:27:35 -05:00
import { setBasePath } from '@onsonr/nebula/ %NPMDIR%/utilities/base-path';
2024-07-17 15:23:16 -04:00
2024-12-06 14:27:35 -05:00
setBasePath('https://cdn.jsdelivr.net/npm/@onsonr/nebula @%VERSION%/%CDNDIR%/');
2024-07-17 15:23:16 -04:00
```
:::tip
2024-12-06 14:27:35 -05:00
If you'd rather not use the CDN for assets, you can create a build task that copies `node_modules/@onsonr/nebula/%NPMDIR%/assets` into a public folder in your app. Then you can point the base path to that folder instead.
2024-07-17 15:23:16 -04:00
:::
## Configuration
Then make sure to apply the custom elements schema as shown below.
```js
import { BrowserModule } from '@angular/platform -browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core ';
import { AppComponent } from './app.component';
@NgModule ({
declarations: [AppComponent],
imports: [BrowserModule],
providers: [],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule {}
```
2024-12-06 14:27:35 -05:00
## Reference Nebula components in your Angular component code
2024-07-17 15:23:16 -04:00
```js
2024-12-06 14:27:35 -05:00
import { SlDrawer } from '@onsonr/nebula ';
2024-07-17 15:23:16 -04:00
@Component ({
selector: 'app-drawer-example',
template: '< div id = "page" >< button ( click )=" showDrawer ()" > Show drawer</ button >< sl-drawer #drawer label = "Drawer" class = "drawer-focus" style = "--size: 50vw" >< p > Drawer content</ p ></ sl-drawer ></ div > '
})
export class DrawerExampleComponent implements OnInit {
// use @ViewChild to get a reference to the #drawer element within component template
@ViewChild ('drawer')
drawer?: ElementRef< SlDrawer > ;
...
constructor(...) {
}
ngOnInit() {
}
...
showDrawer() {
2024-12-06 14:27:35 -05:00
// use nativeElement to access Nebula components
2024-07-17 15:23:16 -04:00
this.drawer?.nativeElement.show();
}
}
```
2024-12-06 14:27:35 -05:00
Now you can start using Nebula components in your app!
2024-07-17 15:23:16 -04:00
:::tip
2024-12-06 14:27:35 -05:00
Are you using Nebula with Angular? [Help us improve this page! ](https://github.com/onsonr/nebula/blob/next/docs/frameworks/angular.md )
2024-07-17 15:23:16 -04:00
:::