Skip to content

Commit b98c746

Browse files
committed
Improve get started guide
1 parent 276ee85 commit b98c746

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

packages/embla-carousel-docs/src/content/pages/get-started/angular.mdx

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ date: 2024-01-05
77

88
import { Tabs } from 'components/Tabs/Tabs'
99
import { TabsItem } from 'components/Tabs/TabsItem'
10+
import { TABS_PACKAGE_MANAGER } from 'consts/tabs'
1011

1112
# Angular
1213

1314
Start by installing the Embla Carousel Angular **npm package** and add it to your dependencies.
1415

15-
<Tabs groupId="package-manager">
16-
<TabsItem label="npm" value="npm">
16+
<Tabs groupId={TABS_PACKAGE_MANAGER.GROUP_ID}>
17+
<TabsItem tab={TABS_PACKAGE_MANAGER.TABS.NPM}>
1718

1819
```shell
1920
npm install embla-carousel-angular --save
2021
```
2122

2223
</TabsItem>
23-
<TabsItem label="yarn" value="yarn">
24+
<TabsItem tab={TABS_PACKAGE_MANAGER.TABS.YARN}>
2425

2526
```shell
2627
yarn add embla-carousel-angular
@@ -33,9 +34,12 @@ Start by installing the Embla Carousel Angular **npm package** and add it to you
3334

3435
Embla Carousel provides the handy `EmblaCarouselDirective` **standalone** directive for seamless integration with Angular. A minimal setup requires an **overflow wrapper** and a **scroll container**. Start by adding the following structure to your carousel:
3536

36-
```typescript highlight={15}
37+
```ts highlight={15}
3738
import { AfterViewInit, Component, ViewChild } from '@angular/core'
38-
import { EmblaCarouselDirective, EmblaCarouselType } from 'embla-carousel-angular'
39+
import {
40+
EmblaCarouselDirective,
41+
EmblaCarouselType
42+
} from 'embla-carousel-angular'
3943

4044
@Component({
4145
selector: 'app-carousel',
@@ -48,21 +52,21 @@ import { EmblaCarouselDirective, EmblaCarouselType } from 'embla-carousel-angula
4852
</div>
4953
</div>
5054
`,
51-
imports: [ EmblaCarouselDirective ],
55+
imports: [EmblaCarouselDirective],
5256
standalone: true
5357
})
5458
export class CarouselComponent implements AfterViewInit {
5559
@ViewChild(EmblaCarouselDirective) emblaRef: EmblaCarouselDirective
56-
60+
5761
private emblaApi?: EmblaCarouselType
5862
private options = { loop: false }
5963

6064
ngAfterViewInit() {
6165
this.emblaApi = this.emblaRef.emblaApi
6266
}
6367
}
64-
6568
```
69+
6670
## Styling the carousel
6771

6872
The element with the classname `embla` is needed to cover the scroll overflow. Its child element with the `container` classname is the scroll body that scrolls the slides. Continue by adding the following **CSS** to these elements:
@@ -84,22 +88,31 @@ The element with the classname `embla` is needed to cover the scroll overflow. I
8488

8589
The `emblaCarousel` directive takes the Embla Carousel [options](/api/options/) as part of its inputs. Additionally, you can access the [API](/api/) by using the `@ViewChild` decorator to access the carousel in `AfterViewInit` hook。
8690

87-
**ATTENTION**: Calling the following embla APIs directly will trigger too much ChangeDetection, which will lead to serious performance issues.
91+
<Admonition type="warning">
92+
**Warning**: Calling the following embla APIs directly will trigger too much
93+
ChangeDetection, which will lead to serious performance issues.
94+
95+
<br />
8896

8997
- `emblaApi.on()`
9098
- `emblaApi.scrollNext()`
9199
- `emblaApi.scrollPrev()`
92100
- `emblaApi.scrollTo()`
93101

102+
</Admonition>
103+
94104
Consider using the following methods which are wrapped with `ngZone.runOutsideAngular()`:
95105

96106
- `EmblaCarouselDirective.scrollPrev()`
97107
- `EmblaCarouselDirective.scrollNext()`
98108
- `EmblaCarouselDirective.scrollTo()`
99109

100-
```typescript highlight={7,19,24-26}
110+
```ts highlight={7,19,24-26}
101111
import { AfterViewInit, Component, ViewChild } from '@angular/core'
102-
import { EmblaCarouselDirective, EmblaCarouselType } from 'embla-carousel-angular'
112+
import {
113+
EmblaCarouselDirective,
114+
EmblaCarouselType
115+
} from 'embla-carousel-angular'
103116

104117
@Component({
105118
selector: 'app-carousel',
@@ -112,29 +125,32 @@ import { EmblaCarouselDirective, EmblaCarouselType } from 'embla-carousel-angula
112125
</div>
113126
</div>
114127
`,
115-
imports: [ EmblaCarouselDirective ],
128+
imports: [EmblaCarouselDirective],
116129
standalone: true
117130
})
118131
export class CarouselComponent implements AfterViewInit {
119132
@ViewChild(EmblaCarouselDirective) emblaRef: EmblaCarouselDirective
120-
133+
121134
private emblaApi?: EmblaCarouselType
122135
private options = { loop: false }
123136

124137
ngAfterViewInit() {
125138
this.emblaApi = this.emblaRef.emblaApi
126139
}
127140
}
128-
129141
```
130142

131143
## Listening the carousel events
132144

133145
The `emblaCarousel` directive also provides a custom event: `emblaChange` that forwards embla events, also wrapped in `ngZone.runOutsideAngular`. You need to listen by passing the specified event names into `subscribeToEvents` input on demand.
134146

135-
```typescript highlight={11,12,30,44-46}
147+
```ts highlight={11,12,30,44-46}
136148
import { AfterViewInit, Component, ViewChild } from '@angular/core'
137-
import { EmblaCarouselDirective, EmblaCarouselType, EmblaEventType } from 'embla-carousel-angular'
149+
import {
150+
EmblaCarouselDirective,
151+
EmblaCarouselType,
152+
EmblaEventType
153+
} from 'embla-carousel-angular'
138154

139155
@Component({
140156
selector: 'app-carousel',
@@ -153,12 +169,12 @@ import { EmblaCarouselDirective, EmblaCarouselType, EmblaEventType } from 'embla
153169
</div>
154170
</div>
155171
`,
156-
imports: [ EmblaCarouselDirective ],
172+
imports: [EmblaCarouselDirective],
157173
standalone: true
158174
})
159175
export class CarouselComponent implements AfterViewInit {
160176
@ViewChild(EmblaCarouselDirective) emblaRef: EmblaCarouselDirective
161-
177+
162178
private emblaApi?: EmblaCarouselType
163179
private options = { loop: false }
164180

@@ -174,34 +190,31 @@ export class CarouselComponent implements AfterViewInit {
174190
'reInit',
175191
'resize',
176192
'scroll'
177-
];
193+
]
178194

179195
onEmblaChanged(event: EmblaEventType): void {
180-
console.log(`Embla event triggered: ${event}`);
196+
console.log(`Embla event triggered: ${event}`)
181197
}
182198

183199
ngAfterViewInit() {
184200
this.emblaApi = this.emblaRef.emblaApi
185201
}
186202
}
187-
188203
```
189204

190-
191205
## Adding plugins
192206

193-
194207
Start by installing the plugin you want to use. In this example, we're going to install the [Autoplay](/plugins/autoplay/) plugin:
195208

196-
<Tabs groupId="package-manager">
197-
<TabsItem label="npm" value="npm">
209+
<Tabs groupId={TABS_PACKAGE_MANAGER.GROUP_ID}>
210+
<TabsItem tab={TABS_PACKAGE_MANAGER.TABS.NPM}>
198211

199212
```shell
200213
npm install embla-carousel-autoplay --save
201214
```
202215

203216
</TabsItem>
204-
<TabsItem label="yarn" value="yarn">
217+
<TabsItem tab={TABS_PACKAGE_MANAGER.TABS.YARN}>
205218

206219
```shell
207220
yarn add embla-carousel-autoplay
@@ -210,13 +223,14 @@ Start by installing the plugin you want to use. In this example, we're going to
210223
</TabsItem>
211224
</Tabs>
212225

213-
214226
The `emblaCarousel` directive inputs also accepts [plugins](/plugins/). Note that plugins need to be passed in an array like so:
215227

216-
217-
```typescript highlight={3,8,24}
228+
```ts highlight={3,8,24}
218229
import { AfterViewInit, Component, ViewChild } from '@angular/core'
219-
import { EmblaCarouselDirective, EmblaCarouselType } from 'embla-carousel-angular'
230+
import {
231+
EmblaCarouselDirective,
232+
EmblaCarouselType
233+
} from 'embla-carousel-angular'
220234
import Autoplay from 'embla-carousel-autoplay'
221235

222236
@Component({
@@ -230,12 +244,12 @@ import Autoplay from 'embla-carousel-autoplay'
230244
</div>
231245
</div>
232246
`,
233-
imports: [ EmblaCarouselDirective ],
247+
imports: [EmblaCarouselDirective],
234248
standalone: true
235249
})
236250
export class CarouselComponent implements AfterViewInit {
237251
@ViewChild(EmblaCarouselDirective) emblaRef: EmblaCarouselDirective
238-
252+
239253
private emblaApi?: EmblaCarouselType
240254
public options = { loop: false }
241255
public plugins = [Autoplay()]
@@ -244,7 +258,6 @@ export class CarouselComponent implements AfterViewInit {
244258
this.emblaApi = this.emblaRef.emblaApi
245259
}
246260
}
247-
248261
```
249262

250263
Congratulations! You just created your first Embla Carousel component.

0 commit comments

Comments
 (0)