Skip to content

docs(config): add code snippets of how to access the mode per framework #4172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/developing/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ import PerPlatformOverridesExample from '@site/docs/developing/config/per-platfo

<PerPlatformOverridesExample />

## Accessing the Mode

In some cases, you may need to access the current Ionic mode programmatically within your application logic. This can be useful for applying conditional behavior, fetching specific assets, or performing other actions based on the active styling mode.

import IonicMode from '@site/static/usage/v8/config/mode/index.md';

<IonicMode />

## Reading the Config (Angular)

Ionic Angular provides a `Config` provider for accessing the Ionic Config.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```html
<ion-button [color]="mode === 'ios' ? 'secondary' : 'tertiary'" [fill]="mode === 'ios' ? 'outline' : 'solid'">
Current mode: {{ mode }}
</ion-button>
```
16 changes: 16 additions & 0 deletions static/usage/v7/config/mode/angular/example_component_ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
```ts
import { Component } from '@angular/core';
import { Config, IonButton } from '@ionic/angular/standalone';

@Component({
selector: 'app-example',
templateUrl: './example.component.html',
imports: [IonButton],
})
export class ExampleComponent {
mode: string;
constructor(public config: Config) {
this.mode = this.config.get('mode');
}
}
```
41 changes: 41 additions & 0 deletions static/usage/v7/config/mode/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ionic Config Mode</title>
<link rel="stylesheet" href="../../common.css" />
<script src="../../common.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@8/dist/ionic/ionic.esm.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@8/css/ionic.bundle.css" />
<style>
.mode-value {
margin-top: 16px;
font-weight: bold;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
gap: 0;
}
</style>
</head>
<body>
<ion-app>
<ion-content>
<div class="container">
<ion-button id="modeButton"></ion-button>
</div>
</ion-content>
</ion-app>
<script type="module">
const modeButton = document.querySelector('#modeButton');
const mode = window.Ionic.config.get('mode') || document.documentElement.getAttribute('mode') || 'md';

modeButton.innerHTML = `Current mode: ${mode}`;
modeButton.setAttribute('color', mode === 'ios' ? 'secondary' : 'tertiary');
modeButton.setAttribute('fill', mode === 'ios' ? 'outline' : 'solid');
</script>
</body>
</html>
24 changes: 24 additions & 0 deletions static/usage/v7/config/mode/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Playground from '@site/src/components/global/Playground';

import javascript from './javascript.md';
import react from './react.md';
import vue from './vue.md';

import angular_example_component_html from './angular/example_component_html.md';
import angular_example_component_ts from './angular/example_component_ts.md';

<Playground
version="7"
code={{
javascript,
react,
vue,
angular: {
files: {
'src/app/example.component.html': angular_example_component_html,
'src/app/example.component.ts': angular_example_component_ts,
},
},
}}
src="usage/v7/config/mode/demo.html"
/>
12 changes: 12 additions & 0 deletions static/usage/v7/config/mode/javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```html
<ion-button id="modeButton"></ion-button>

<script>
const modeButton = document.querySelector('#modeButton');
const mode = window.Ionic.config.get('mode') || document.documentElement.getAttribute('mode') || 'md';

modeButton.innerHTML = `Current mode: ${mode}`;
modeButton.setAttribute('color', mode === 'ios' ? 'secondary' : 'tertiary');
modeButton.setAttribute('fill', mode === 'ios' ? 'outline' : 'solid');
</script>
```
25 changes: 25 additions & 0 deletions static/usage/v7/config/mode/react.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
```tsx
import React, { useState, useEffect } from 'react';
import { IonButton } from '@ionic/react';
import { getMode } from '@ionic/core';

function Example() {
const [mode, setMode] = useState('');

useEffect(() => {
const mode = getMode() || 'md';
setMode(mode);
}, []);

const color = mode === 'ios' ? 'secondary' : 'tertiary';
const fill = mode === 'ios' ? 'outline' : 'solid';

return (
<IonButton color={color} fill={fill}>
Current mode: {mode}
</IonButton>
);
}

export default Example;
```
20 changes: 20 additions & 0 deletions static/usage/v7/config/mode/vue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```html
<template>
<ion-button :color="color" :fill="fill"> Current mode: {{ mode }} </ion-button>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { IonButton } from '@ionic/vue';
import { getMode } from '@ionic/core';

const mode = ref('');

const color = computed(() => (mode.value === 'ios' ? 'secondary' : 'tertiary'));
const fill = computed(() => (mode.value === 'ios' ? 'outline' : 'solid'));

onMounted(() => {
mode.value = getMode() || 'md';
});
</script>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```html
<ion-button [color]="mode === 'ios' ? 'secondary' : 'tertiary'" [fill]="mode === 'ios' ? 'outline' : 'solid'">
Current mode: {{ mode }}
</ion-button>
```
16 changes: 16 additions & 0 deletions static/usage/v8/config/mode/angular/example_component_ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
```ts
import { Component } from '@angular/core';
import { Config, IonButton } from '@ionic/angular/standalone';

@Component({
selector: 'app-example',
templateUrl: './example.component.html',
imports: [IonButton],
})
export class ExampleComponent {
mode: string;
constructor(public config: Config) {
this.mode = this.config.get('mode');
}
}
```
41 changes: 41 additions & 0 deletions static/usage/v8/config/mode/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ionic Config Mode</title>
<link rel="stylesheet" href="../../common.css" />
<script src="../../common.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@8/dist/ionic/ionic.esm.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@8/css/ionic.bundle.css" />
<style>
.mode-value {
margin-top: 16px;
font-weight: bold;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
gap: 0;
}
</style>
</head>
<body>
<ion-app>
<ion-content>
<div class="container">
<ion-button id="modeButton"></ion-button>
</div>
</ion-content>
</ion-app>
<script type="module">
const modeButton = document.querySelector('#modeButton');
const mode = window.Ionic.config.get('mode') || document.documentElement.getAttribute('mode') || 'md';

modeButton.innerHTML = `Current mode: ${mode}`;
modeButton.setAttribute('color', mode === 'ios' ? 'secondary' : 'tertiary');
modeButton.setAttribute('fill', mode === 'ios' ? 'outline' : 'solid');
</script>
</body>
</html>
24 changes: 24 additions & 0 deletions static/usage/v8/config/mode/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Playground from '@site/src/components/global/Playground';

import javascript from './javascript.md';
import react from './react.md';
import vue from './vue.md';

import angular_example_component_html from './angular/example_component_html.md';
import angular_example_component_ts from './angular/example_component_ts.md';

<Playground
version="8"
code={{
javascript,
react,
vue,
angular: {
files: {
'src/app/example.component.html': angular_example_component_html,
'src/app/example.component.ts': angular_example_component_ts,
},
},
}}
src="usage/v8/config/mode/demo.html"
/>
12 changes: 12 additions & 0 deletions static/usage/v8/config/mode/javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```html
<ion-button id="modeButton"></ion-button>

<script>
const modeButton = document.querySelector('#modeButton');
const mode = window.Ionic.config.get('mode') || document.documentElement.getAttribute('mode') || 'md';

modeButton.innerHTML = `Current mode: ${mode}`;
modeButton.setAttribute('color', mode === 'ios' ? 'secondary' : 'tertiary');
modeButton.setAttribute('fill', mode === 'ios' ? 'outline' : 'solid');
</script>
```
24 changes: 24 additions & 0 deletions static/usage/v8/config/mode/react.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
```tsx
import React, { useState, useEffect } from 'react';
import { IonButton } from '@ionic/react';
import { getMode } from '@ionic/core';

function Example() {
const [mode, setMode] = useState('');

useEffect(() => {
const mode = getMode() || 'md';
setMode(mode);
}, []);

const color = mode === 'ios' ? 'secondary' : 'tertiary';
const fill = mode === 'ios' ? 'outline' : 'solid';

return (
<IonButton color={color} fill={fill}>
Current mode: {mode}
</IonButton>
);
}
export default Example;
```
20 changes: 20 additions & 0 deletions static/usage/v8/config/mode/vue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```html
<template>
<ion-button :color="color" :fill="fill"> Current mode: {{ mode }} </ion-button>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { IonButton } from '@ionic/vue';
import { getMode } from '@ionic/core';

const mode = ref('');

const color = computed(() => (mode.value === 'ios' ? 'secondary' : 'tertiary'));
const fill = computed(() => (mode.value === 'ios' ? 'outline' : 'solid'));

onMounted(() => {
mode.value = getMode() || 'md';
});
</script>
```
8 changes: 8 additions & 0 deletions versioned_docs/version-v7/developing/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ import PerPlatformOverridesExample from '@site/docs/developing/config/per-platfo

<PerPlatformOverridesExample />

## Accessing the Mode

In some cases, you may need to access the current Ionic mode programmatically within your application logic. This can be useful for applying conditional behavior, fetching specific assets, or performing other actions based on the active styling mode.

import IonicMode from '@site/static/usage/v7/config/mode/index.md';

<IonicMode />

## Reading the Config (Angular)

Ionic Angular provides a `Config` provider for accessing the Ionic Config.
Expand Down