Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 7 additions & 1 deletion docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Slots from '@ionic-internal/component-api/v8/app/slots.md';

import EncapsulationPill from '@components/page/api/EncapsulationPill';


App is a container element for an Ionic application. There should only be one `<ion-app>` element per project. An app can have many Ionic components including menus, headers, content, and footers. The overlay components get appended to the `<ion-app>` when they are presented.

Using `ion-app` enables the following behaviors:
Expand All @@ -27,6 +26,13 @@ Using `ion-app` enables the following behaviors:
* [Ripple effect](./ripple-effect) when activating buttons on Material Design mode
* Other tap and focus utilities which make the experience of using an Ionic app feel more native

## Programmatic Focus

As mentioned earlier, the `ion-app` component provides focus utilities for Ionic components with the `ion-focusable` class. The `setFocus` method allows you to programmatically focus an element in response to user actions. However, it should not be used when the element is focused due to a keyboard event, as the focus utility will handle that automatically.

import SetFocus from '@site/static/usage/v8/app/set-focus/index.md';

<SetFocus />

## Properties
<Props />
Expand Down
12 changes: 12 additions & 0 deletions static/usage/v8/app/set-focus/angular/example_component_html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```html
<ion-button id="buttonToFocus" fill="outline">Button</ion-button>

<ion-radio-group value="a">
<ion-radio id="radioToFocus" value="a">Radio</ion-radio>
</ion-radio-group>

<br />

<ion-button (click)="focusElement('#buttonToFocus')">Focus Button</ion-button>
<ion-button (click)="focusElement('#radioToFocus')">Focus Radio</ion-button>
```
19 changes: 19 additions & 0 deletions static/usage/v8/app/set-focus/angular/example_component_ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
```ts
import { Component } from '@angular/core';

@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
styleUrls: ['example.component.css'],
})
export class ExampleComponent {
focusElement(id: string) {
const el = document.querySelector(id) as HTMLElement;

const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
}
}
```
49 changes: 49 additions & 0 deletions static/usage/v8/app/set-focus/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>App</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>
.container {
display: grid;
grid-template-columns: repeat(2, auto);
column-gap: 10px;
justify-items: center;
}
</style>
</head>

<body>
<ion-app>
<ion-content>
<div class="container">
<ion-button id="buttonToFocus" fill="outline">Button</ion-button>

<ion-radio-group value="a">
<ion-radio id="radioToFocus" value="a">Radio</ion-radio>
</ion-radio-group>

<ion-button onClick="focusElement('#buttonToFocus')">Focus Button</ion-button>
<ion-button onClick="focusElement('#radioToFocus')">Focus Radio</ion-button>
</div>

<script>
function focusElement(id) {
const el = document.querySelector(id);

const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
}
</script>
</ion-content>
</ion-app>
</body>
</html>
25 changes: 25 additions & 0 deletions static/usage/v8/app/set-focus/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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/app/set-focus/demo.html"
size="sm"
/>
23 changes: 23 additions & 0 deletions static/usage/v8/app/set-focus/javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
```html
<ion-button id="buttonToFocus" fill="outline">Button</ion-button>

<ion-radio-group value="a">
<ion-radio id="radioToFocus" value="a">Radio</ion-radio>
</ion-radio-group>

<br />

<ion-button onClick="focusElement('#buttonToFocus')">Focus Button</ion-button>
<ion-button onClick="focusElement('#radioToFocus')">Focus Radio</ion-button>

<script>
function focusElement(id) {
const el = document.querySelector(id);

const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
}
</script>
```
35 changes: 35 additions & 0 deletions static/usage/v8/app/set-focus/react.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
```tsx
import React from 'react';
import { IonButton, IonRadio, IonRadioGroup } from '@ionic/react';

function Example() {
function focusElement(id: string) {
const el = document.querySelector(id) as HTMLElement;

const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
}

return (
<>
<IonButton id="buttonToFocus" fill="outline">
Button
</IonButton>

<IonRadioGroup value="a">
<IonRadio id="radioToFocus" value="a">
Radio
</IonRadio>
</IonRadioGroup>

<br />

<IonButton onClick={() => focusElement('#buttonToFocus')}>Focus Button</IonButton>
<IonButton onClick={() => focusElement('#radioToFocus')}>Focus Radio</IonButton>
</>
);
}
export default Example;
```
33 changes: 33 additions & 0 deletions static/usage/v8/app/set-focus/vue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
```html
<template>
<ion-button id="buttonToFocus" fill="outline">Button</ion-button>

<ion-radio-group value="a">
<ion-radio id="radioToFocus" value="a">Radio</ion-radio>
</ion-radio-group>

<br />

<ion-button @click="focusElement('#buttonToFocus')">Focus Button</ion-button>
<ion-button @click="focusElement('#radioToFocus')">Focus Radio</ion-button>
</template>

<script lang="ts">
import { IonButton, IonRadio, IonRadioGroup } from '@ionic/vue';
import { defineComponent } from 'vue';

export default defineComponent({
components: { IonButton, IonRadio, IonRadioGroup },
methods: {
focusElement(id) {
const el = document.querySelector(id);

const app = el.closest('ion-app');
if (app) {
app.setFocus([el]);
}
},
},
});
</script>
```