|
| 1 | +# Config |
| 2 | + |
| 3 | +Ionic Config provides a way to change the properties of components globally across an app. It can set the app mode, tab button layout, animations, and more. |
| 4 | + |
| 5 | +## Global Config |
| 6 | + |
| 7 | +To override the initial Ionic config for the app, provide a config in `IonicModule.forRoot` in the `app.module.ts` file. |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { IonicModule } from '@ionic/angular'; |
| 11 | + |
| 12 | +@NgModule({ |
| 13 | + ... |
| 14 | + imports: [ |
| 15 | + BrowserModule, |
| 16 | + IonicModule.forRoot({ |
| 17 | + rippleEffect: false, |
| 18 | + mode: 'md' |
| 19 | + }), |
| 20 | + AppRoutingModule |
| 21 | + ], |
| 22 | + ... |
| 23 | +}) |
| 24 | +``` |
| 25 | + |
| 26 | +In the above example, we are disabling the Material Design ripple effect across the app, as well as forcing the mode to be Material Design. |
| 27 | + |
| 28 | + |
| 29 | +## Per-Component Config |
| 30 | + |
| 31 | +Ionic Config is not reactive, so it is recommended to use a component's properties when you want to override its default behavior rather than set its config globally. |
| 32 | + |
| 33 | +```typescript |
| 34 | +import { IonicModule } from '@ionic/angular'; |
| 35 | + |
| 36 | +@NgModule({ |
| 37 | + ... |
| 38 | + imports: [ |
| 39 | + BrowserModule, |
| 40 | + IonicModule.forRoot({ |
| 41 | + backButtonText: 'Go Back' |
| 42 | + }), |
| 43 | + AppRoutingModule |
| 44 | + ], |
| 45 | + ... |
| 46 | +}) |
| 47 | +``` |
| 48 | + |
| 49 | +This will set the default text for `ion-back-button` to `Go Back`. However, if you were to change the value of the `backButtonText` config to `Do Not Go Back`, the `ion-back-button` default text would still default to `Go Back` as the component has already been initialized and rendered. Instead, it is recommended to use the `text` property on `ion-back-button`. |
| 50 | + |
| 51 | +```html |
| 52 | +<ion-back-button [text]="getBackButtonText()"></ion-back-button> |
| 53 | +``` |
| 54 | + |
| 55 | +In this example we have used our `ion-back-button` in such a way that the text can be dynamically updated if there were to be a change that warranted it, such as a language or locale change. The `getBackButtonText` method would be responsible for returning the correct text. |
| 56 | + |
| 57 | +## Per-Platform Config |
| 58 | + |
| 59 | +Ionic Config can also be set on a per-platform basis. For example, this allows you to disable animations if the app is being run in a browser on a potentially slower device. Developers can take advantage of the Platform utilities to accomplish this. |
| 60 | + |
| 61 | +Since the config is set at runtime, you will not have access to the Platform Dependency Injection. Instead, you can use the underlying functions that the provider uses directly. |
| 62 | + |
| 63 | +In the following example, we are disabling all animations in our Ionic app only if the app is running in a mobile web browser. |
| 64 | +The `isPlatform()` call returns `true` or `false` based upon the platform that is passed in. See the [Platform Documentation](platform.md#platforms) for a list of possible values. |
| 65 | + |
| 66 | + |
| 67 | +```typescript |
| 68 | +import { isPlatform, IonicModule } from '@ionic/angular'; |
| 69 | + |
| 70 | +@NgModule({ |
| 71 | + ... |
| 72 | + imports: [ |
| 73 | + BrowserModule, |
| 74 | + IonicModule.forRoot({ |
| 75 | + animated: !isPlatform('mobileweb') |
| 76 | + }), |
| 77 | + AppRoutingModule |
| 78 | + ], |
| 79 | + ... |
| 80 | +}) |
| 81 | +``` |
| 82 | + |
| 83 | +The next example allows you to set an entirely different configuration based upon the platform, falling back to a default config if no platforms match: |
| 84 | + |
| 85 | +```typescript |
| 86 | +import { isPlatform, IonicModule } from '@ionic/angular'; |
| 87 | + |
| 88 | +const getConfig = () => { |
| 89 | + if (isPlatform('hybrid')) { |
| 90 | + return { |
| 91 | + backButtonText: 'Previous', |
| 92 | + tabButtonLayout: 'label-hide' |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + menuIcon: 'ellipsis-vertical' |
| 98 | + } |
| 99 | +} |
| 100 | +@NgModule({ |
| 101 | + ... |
| 102 | + imports: [ |
| 103 | + BrowserModule, |
| 104 | + IonicModule.forRoot(getConfig()), |
| 105 | + AppRoutingModule |
| 106 | + ], |
| 107 | + ... |
| 108 | +}) |
| 109 | +``` |
| 110 | + |
| 111 | +Finally, this example allows you to accumulate a config object based upon different platform requirements: |
| 112 | + |
| 113 | +```typescript |
| 114 | +import { isPlatform, IonicModule } from '@ionic/angular'; |
| 115 | + |
| 116 | +const getConfig = () => { |
| 117 | + let config = { |
| 118 | + animated: false |
| 119 | + }; |
| 120 | + |
| 121 | + if (isPlatform('iphone')) { |
| 122 | + config = { |
| 123 | + ...config, |
| 124 | + backButtonText: 'Previous' |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return config; |
| 129 | +} |
| 130 | +@NgModule({ |
| 131 | + ... |
| 132 | + imports: [ |
| 133 | + BrowserModule, |
| 134 | + IonicModule.forRoot(getConfig()), |
| 135 | + AppRoutingModule |
| 136 | + ], |
| 137 | + ... |
| 138 | +}) |
| 139 | +``` |
| 140 | + |
| 141 | +## Config Options |
| 142 | + |
| 143 | +Below is a list of config options that Ionic uses. |
| 144 | + |
| 145 | +| Config | Type | Description | |
| 146 | +|--------------------------|--------------------|----------------------------------------------------------------------------------------------------------| |
| 147 | +| `actionSheetEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-action-sheet`, overriding the default "animation". |
| 148 | +| `actionSheetLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-action-sheet`, overriding the default "animation". |
| 149 | +| `alertEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-alert`, overriding the default "animation". |
| 150 | +| `alertLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-alert`, overriding the default "animation". |
| 151 | +| `animated` | `boolean` | If `true`, Ionic will enable all animations and transitions across the app. |
| 152 | +| `backButtonIcon` | `string` | Overrides the default icon in all `<ion-back-button>` components. |
| 153 | +| `backButtonText` | `string` | Overrides the default text in all `<ion-back-button>` components. |
| 154 | +| `hardwareBackButton` | `boolean` | If `true`, Ionic will respond to the hardware back button in an Android device. |
| 155 | +| `infiniteLoadingSpinner` | `SpinnerTypes` | Overrides the default spinner type in all `<ion-infinite-scroll-content>` components. |
| 156 | +| `loadingEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-loading`, overriding the default "animation". |
| 157 | +| `loadingLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-loading`, overriding the default "animation". |
| 158 | +| `loadingSpinner` | `SpinnerTypes` | Overrides the default spinner for all `ion-loading` overlays. |
| 159 | +| `menuIcon` | `string` | Overrides the default icon in all `<ion-menu-button>` components. |
| 160 | +| `menuType` | `string` | Overrides the default menu type for all `<ion-menu>` components. |
| 161 | +| `modalEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-modal`, overriding the default "animation". |
| 162 | +| `modalLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-modal`, overriding the default "animation". |
| 163 | +| `mode` | `Mode` | The mode determines which platform styles to use for the whole application. |
| 164 | +| `navAnimation` | `AnimationBuilder` | Overrides the default "animation" of all `ion-nav` and `ion-router-outlet` across the whole application. |
| 165 | +| `pickerEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-picker`, overriding the default "animation". |
| 166 | +| `pickerLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-picker`, overriding the default "animation". |
| 167 | +| `popoverEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-popover`, overriding the default "animation". |
| 168 | +| `popoverLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-popover`, overriding the default "animation". |
| 169 | +| `refreshingIcon` | `string` | Overrides the default icon in all `<ion-refresh-content>` components. |
| 170 | +| `refreshingSpinner` | `SpinnerTypes` | Overrides the default spinner type in all `<ion-refresh-content>` components. |
| 171 | +| `sanitizerEnabled` | `boolean` | If `true`, Ionic will enable a basic DOM sanitizer on component properties that accept custom HTML. |
| 172 | +| `spinner` | `SpinnerTypes` | Overrides the default spinner in all `<ion-spinner>` components. |
| 173 | +| `statusTap` | `boolean` | If `true`, clicking or tapping the status bar will cause the content to scroll to the top. |
| 174 | +| `swipeBackEnabled` | `boolean` | If `true`, Ionic will enable the "swipe-to-go-back" gesture across the application. |
| 175 | +| `tabButtonLayout` | `TabButtonLayout` | Overrides the default "layout" of all `ion-bar-button` across the whole application. |
| 176 | +| `toastEnter` | `AnimationBuilder` | Provides a custom enter animation for all `ion-toast`, overriding the default "animation". |
| 177 | +| `toastLeave` | `AnimationBuilder` | Provides a custom leave animation for all `ion-toast`, overriding the default "animation". |
0 commit comments