Skip to content

Commit 30d1484

Browse files
cyokodogcyokodog
authored andcommitted
第10回デモ追加(微調整)
1 parent c73f95a commit 30d1484

16 files changed

Lines changed: 136 additions & 99 deletions

File tree

10/03_focus_control/public/dist/index.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ globalThis['__css-content-808794906c4839b82ed240e38daf1f1c__']=".App-module__gPM
2020
return el;
2121
};
2222

23-
// src/getFocusHandler.ts
24-
var getFocusHandler = (node) => {
25-
return (selector, cursorPosition = 0) => {
26-
const target = node.querySelector(selector) || node.parentElement?.querySelector(selector);
27-
if (!target) return;
28-
target?.focus();
29-
if (cursorPosition) {
30-
target?.setSelectionRange(cursorPosition, cursorPosition);
31-
}
32-
};
23+
// src/setFocus.ts
24+
var setFocus = (node, selector, cursorPosition = 0) => {
25+
const target = node.querySelector(selector) || node.parentElement?.querySelector(selector);
26+
if (!target) return;
27+
target?.focus();
28+
if (cursorPosition) {
29+
target?.setSelectionRange(cursorPosition, cursorPosition);
30+
}
3331
};
3432

3533
// src/domReplacer.ts
@@ -42,11 +40,10 @@ globalThis['__css-content-808794906c4839b82ed240e38daf1f1c__']=".App-module__gPM
4240
}
4341
function render() {
4442
const id = document.activeElement?.id;
45-
const selectionStart = document.activeElement?.selectionStart;
43+
const selectionStart = document.activeElement?.selectionStart || 0;
4644
const newDom = replaceDom();
47-
const focus = getFocusHandler(newDom);
48-
if (id && focus) {
49-
focus(`#${id}`, selectionStart);
45+
if (id) {
46+
setFocus(newDom, `#${id}`, selectionStart);
5047
}
5148
}
5249
return {
@@ -103,7 +100,7 @@ globalThis['__css-content-808794906c4839b82ed240e38daf1f1c__']=".App-module__gPM
103100
const inputDom = /* @__PURE__ */ h(
104101
"input",
105102
{
106-
id: "name-input",
103+
id: "value-input",
107104
class: App_default._Input,
108105
value: name,
109106
onKeyup: (e) => {

10/03_focus_control/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const App = () => {
88
const appReplacer = domReplacer(() => {
99
const inputDom = (
1010
<input
11-
id="name-input"
11+
id="value-input"
1212
class={styles._Input}
1313
value={name}
1414
onKeyup={(e: KeyboardEvent) => {

10/03_focus_control/src/domReplacer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getFocusHandler } from './getFocusHandler.js';
1+
import { setFocus } from './setFocus.js';
22

33
export const domReplacer = (domFactory: () => HTMLElement) => {
44
let dom = domFactory();
@@ -12,13 +12,12 @@ export const domReplacer = (domFactory: () => HTMLElement) => {
1212
function render() {
1313
// DOMを書き換える前にフォーカスしていた要素のidとselectionStartを記録
1414
const id = document.activeElement?.id;
15-
const selectionStart = (document.activeElement as any)?.selectionStart;
15+
const selectionStart = (document.activeElement as any)?.selectionStart || 0;
1616
const newDom = replaceDom();
1717

1818
// 新しいDOM内に記録したidを持つ要素があればフォーカスする
19-
const focus = getFocusHandler(newDom);
20-
if (id && focus) {
21-
focus(`#${id}`, selectionStart);
19+
if (id) {
20+
setFocus(newDom, `#${id}`, selectionStart);
2221
}
2322
}
2423

10/03_focus_control/src/getFocusHandler.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const setFocus = (
2+
node: HTMLElement,
3+
selector: string,
4+
cursorPosition: number = 0
5+
) => {
6+
const target =
7+
node.querySelector(selector) ||
8+
node.parentElement?.querySelector(selector);
9+
if (!target) return;
10+
11+
(target as any)?.focus();
12+
if (cursorPosition) {
13+
(target as any)?.setSelectionRange(cursorPosition, cursorPosition);
14+
}
15+
};

10/04_focus_animation_problem/public/dist/index.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ globalThis['__css-content-e51d94e970ab9519dc972e19df36829d__']=".App-module__gPM
2020
return el;
2121
};
2222

23-
// src/getFocusHandler.ts
24-
var getFocusHandler = (node) => {
25-
return (selector, cursorPosition = 0) => {
26-
const target = node.querySelector(selector) || node.parentElement?.querySelector(selector);
27-
if (!target) return;
28-
target?.focus();
29-
if (cursorPosition) {
30-
target?.setSelectionRange(cursorPosition, cursorPosition);
31-
}
32-
};
23+
// src/setFocus.ts
24+
var setFocus = (node, selector, cursorPosition = 0) => {
25+
const target = node.querySelector(selector) || node.parentElement?.querySelector(selector);
26+
if (!target) return;
27+
target?.focus();
28+
if (cursorPosition) {
29+
target?.setSelectionRange(cursorPosition, cursorPosition);
30+
}
3331
};
3432

3533
// src/domReplacer.ts
@@ -42,11 +40,10 @@ globalThis['__css-content-e51d94e970ab9519dc972e19df36829d__']=".App-module__gPM
4240
}
4341
function render() {
4442
const id = document.activeElement?.id;
45-
const selectionStart = document.activeElement?.selectionStart;
43+
const selectionStart = document.activeElement?.selectionStart || 0;
4644
const newDom = replaceDom();
47-
const focus = getFocusHandler(newDom);
48-
if (id && focus) {
49-
focus(`#${id}`, selectionStart);
45+
if (id) {
46+
setFocus(newDom, `#${id}`, selectionStart);
5047
}
5148
}
5249
return {
@@ -104,7 +101,7 @@ globalThis['__css-content-e51d94e970ab9519dc972e19df36829d__']=".App-module__gPM
104101
const inputDom = /* @__PURE__ */ h(
105102
"input",
106103
{
107-
id: "name-input",
104+
id: "value-input",
108105
class: App_default._Input,
109106
value: name,
110107
onKeyup: (e) => {

10/04_focus_animation_problem/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const App = () => {
88
const appReplacer = domReplacer(() => {
99
const inputDom = (
1010
<input
11-
id="name-input"
11+
id="value-input"
1212
class={styles._Input}
1313
value={name}
1414
onKeyup={(e: KeyboardEvent) => {

10/04_focus_animation_problem/src/domReplacer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getFocusHandler } from './getFocusHandler.js';
1+
import { setFocus } from './setFocus.js';
22

33
export const domReplacer = (domFactory: () => HTMLElement) => {
44
let dom = domFactory();
@@ -12,13 +12,12 @@ export const domReplacer = (domFactory: () => HTMLElement) => {
1212
function render() {
1313
// DOMを書き換える前にフォーカスしていた要素のidとselectionStartを記録
1414
const id = document.activeElement?.id;
15-
const selectionStart = (document.activeElement as any)?.selectionStart;
15+
const selectionStart = (document.activeElement as any)?.selectionStart || 0;
1616
const newDom = replaceDom();
1717

1818
// 新しいDOM内に記録したidを持つ要素があればフォーカスする
19-
const focus = getFocusHandler(newDom);
20-
if (id && focus) {
21-
focus(`#${id}`, selectionStart);
19+
if (id) {
20+
setFocus(newDom, `#${id}`, selectionStart);
2221
}
2322
}
2423

10/04_focus_animation_problem/src/getFocusHandler.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const setFocus = (
2+
node: HTMLElement,
3+
selector: string,
4+
cursorPosition: number = 0
5+
) => {
6+
const target =
7+
node.querySelector(selector) ||
8+
node.parentElement?.querySelector(selector);
9+
if (!target) return;
10+
11+
(target as any)?.focus();
12+
if (cursorPosition) {
13+
(target as any)?.setSelectionRange(cursorPosition, cursorPosition);
14+
}
15+
};

0 commit comments

Comments
 (0)