dokodemo-ui 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 tamoco-mocomoco
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # dokodemo-ui
2
+
3
+ ドラッグで自由に移動できるWeb Component
4
+
5
+ ## デモ
6
+
7
+ https://tamoco-mocomoco.github.io/dokodemo-ui/
8
+
9
+ ## 特徴
10
+
11
+ - 任意のHTML要素をドラッグで移動可能に
12
+ - 端を掴んでドラッグ(iframe内のコンテンツも操作可能)
13
+ - 閉じるボタン(スタイル・位置カスタマイズ可能)
14
+ - リサイズ機能
15
+ - 位置プリセット(top-left, top-right, bottom-left, bottom-right, center)
16
+ - ウィンドウリサイズ追従
17
+ - マウス・タッチ対応
18
+
19
+ ## インストール
20
+
21
+ ### CDN(jsDelivr)
22
+
23
+ ```html
24
+ <script type="module">
25
+ import 'https://cdn.jsdelivr.net/gh/tamoco-mocomoco/dokodemo-ui@v1.0.0/dokodemo-ui.js';
26
+ </script>
27
+ ```
28
+
29
+ ### npm
30
+
31
+ ```bash
32
+ npm install dokodemo-ui
33
+ ```
34
+
35
+ ```javascript
36
+ import 'dokodemo-ui';
37
+ ```
38
+
39
+ ## 使い方
40
+
41
+ ### 基本
42
+
43
+ ```html
44
+ <dokodemo-ui>
45
+ <button>移動できるボタン</button>
46
+ </dokodemo-ui>
47
+ ```
48
+
49
+ ### 閉じるボタン付き
50
+
51
+ ```html
52
+ <dokodemo-ui closable>
53
+ <div class="card">閉じることができるカード</div>
54
+ </dokodemo-ui>
55
+ ```
56
+
57
+ ### 位置指定
58
+
59
+ ```html
60
+ <dokodemo-ui position="bottom-right" padding="20">
61
+ <div>右下に配置</div>
62
+ </dokodemo-ui>
63
+ ```
64
+
65
+ ### リサイズ可能
66
+
67
+ ```html
68
+ <dokodemo-ui resizable style="width: 300px; height: 200px;">
69
+ <iframe src="https://example.com"></iframe>
70
+ </dokodemo-ui>
71
+ ```
72
+
73
+ ### フルオプション
74
+
75
+ ```html
76
+ <dokodemo-ui
77
+ closable
78
+ close-style="simple"
79
+ close-position="outside"
80
+ close-color="#333"
81
+ resizable
82
+ position="top-right"
83
+ padding="30"
84
+ >
85
+ <div>カスタマイズされたUI</div>
86
+ </dokodemo-ui>
87
+ ```
88
+
89
+ ## 属性
90
+
91
+ | 属性 | 説明 | デフォルト |
92
+ |------|------|-----------|
93
+ | `closable` | 閉じるボタンを表示 | - |
94
+ | `close-style` | 閉じるボタンのスタイル(`circle` / `simple`) | `circle` |
95
+ | `close-position` | 閉じるボタンの位置(`inside` / `outside`) | `inside` |
96
+ | `close-color` | 閉じるボタンの色 | `#ff5f57` |
97
+ | `resizable` | リサイズ可能にする | - |
98
+ | `position` | 初期位置(`top-left` / `top-right` / `bottom-left` / `bottom-right` / `center`) | - |
99
+ | `padding` | 角からの距離(px) | `20` |
100
+ | `x` | X座標を直接指定(px) | - |
101
+ | `y` | Y座標を直接指定(px) | - |
102
+
103
+ ## イベント
104
+
105
+ | イベント | 説明 |
106
+ |---------|------|
107
+ | `close` | 閉じるボタンがクリックされた時 |
108
+
109
+ ```javascript
110
+ document.querySelector('dokodemo-ui').addEventListener('close', () => {
111
+ console.log('閉じられました');
112
+ });
113
+ ```
114
+
115
+ ## メソッド
116
+
117
+ | メソッド | 説明 |
118
+ |---------|------|
119
+ | `show()` | 要素を表示 |
120
+ | `hide()` | 要素を非表示 |
121
+
122
+ ```javascript
123
+ const ui = document.querySelector('dokodemo-ui');
124
+ ui.hide(); // 非表示
125
+ ui.show(); // 再表示
126
+ ```
127
+
128
+ ## 操作方法
129
+
130
+ - **移動**: 要素の端(8px)をドラッグ
131
+ - **リサイズ**: 右下のハンドルをドラッグ(`resizable`属性が必要)
132
+ - **閉じる**: 閉じるボタンをクリック(`closable`属性が必要)
133
+
134
+ ## ブラウザ対応
135
+
136
+ - Chrome / Edge(最新版)
137
+ - Firefox(最新版)
138
+ - Safari(最新版)
139
+
140
+ ## ライセンス
141
+
142
+ MIT
@@ -0,0 +1,90 @@
1
+ /**
2
+ * <dokodemo-ui> - ドラッグで自由に移動できるWeb Component
3
+ *
4
+ * 使い方:
5
+ * <dokodemo-ui>
6
+ * <button>移動できるボタン</button>
7
+ * </dokodemo-ui>
8
+ *
9
+ * オプション:
10
+ * - closable: バツボタンを表示して非表示にできるようにする
11
+ * <dokodemo-ui closable>...</dokodemo-ui>
12
+ * - position: 初期位置を指定(top-left, top-right, bottom-left, bottom-right, center)
13
+ * <dokodemo-ui position="bottom-right">...</dokodemo-ui>
14
+ * - padding: 角からの距離(デフォルト: 20px)
15
+ * <dokodemo-ui position="bottom-right" padding="30">...</dokodemo-ui>
16
+ * - x, y: 初期位置を直接指定(単位はpx)※positionより優先
17
+ * <dokodemo-ui x="100" y="200">...</dokodemo-ui>
18
+ */
19
+ type Position = "top-left" | "top-right" | "bottom-left" | "bottom-right" | "center";
20
+ declare class DokodemoUI extends HTMLElement {
21
+ private isDragging;
22
+ private isResizing;
23
+ private hasMoved;
24
+ private dragOffsetX;
25
+ private dragOffsetY;
26
+ private resizeStartX;
27
+ private resizeStartY;
28
+ private resizeStartWidth;
29
+ private resizeStartHeight;
30
+ private positionInitialized;
31
+ private iframePointerEvents;
32
+ private edgeSize;
33
+ static get observedAttributes(): string[];
34
+ get closable(): boolean;
35
+ set closable(value: boolean);
36
+ get position(): Position | null;
37
+ set position(value: Position | null);
38
+ get padding(): number;
39
+ set padding(value: number);
40
+ get closeColor(): string;
41
+ set closeColor(value: string);
42
+ get closeStyle(): "circle" | "simple";
43
+ set closeStyle(value: "circle" | "simple");
44
+ get closePosition(): "inside" | "outside";
45
+ set closePosition(value: "inside" | "outside");
46
+ get resizable(): boolean;
47
+ set resizable(value: boolean);
48
+ get x(): number | null;
49
+ set x(value: number | null);
50
+ get y(): number | null;
51
+ set y(value: number | null);
52
+ constructor();
53
+ connectedCallback(): void;
54
+ attributeChangedCallback(): void;
55
+ private render;
56
+ private initializePosition;
57
+ private applyPosition;
58
+ private setupDrag;
59
+ private isNearEdge;
60
+ private onMouseMoveForCursor;
61
+ private onMouseLeaveForCursor;
62
+ private onClickCapture;
63
+ private onMouseDown;
64
+ private onTouchStart;
65
+ private shouldIgnoreEvent;
66
+ private setupCloseButton;
67
+ show(): void;
68
+ hide(): void;
69
+ private startDrag;
70
+ private disableIframePointerEvents;
71
+ private restoreIframePointerEvents;
72
+ private onMouseMove;
73
+ private onTouchMove;
74
+ private updatePosition;
75
+ private onMouseUp;
76
+ private onTouchEnd;
77
+ private endDrag;
78
+ private setupResize;
79
+ private onResizeMouseDown;
80
+ private onResizeTouchStart;
81
+ private startResize;
82
+ private onResizeMouseMove;
83
+ private onResizeTouchMove;
84
+ private updateSize;
85
+ private onResizeMouseUp;
86
+ private onResizeTouchEnd;
87
+ private endResize;
88
+ disconnectedCallback(): void;
89
+ }
90
+ export { DokodemoUI };
package/dokodemo-ui.js ADDED
@@ -0,0 +1,579 @@
1
+ /**
2
+ * <dokodemo-ui> - ドラッグで自由に移動できるWeb Component
3
+ *
4
+ * 使い方:
5
+ * <dokodemo-ui>
6
+ * <button>移動できるボタン</button>
7
+ * </dokodemo-ui>
8
+ *
9
+ * オプション:
10
+ * - closable: バツボタンを表示して非表示にできるようにする
11
+ * <dokodemo-ui closable>...</dokodemo-ui>
12
+ * - position: 初期位置を指定(top-left, top-right, bottom-left, bottom-right, center)
13
+ * <dokodemo-ui position="bottom-right">...</dokodemo-ui>
14
+ * - padding: 角からの距離(デフォルト: 20px)
15
+ * <dokodemo-ui position="bottom-right" padding="30">...</dokodemo-ui>
16
+ * - x, y: 初期位置を直接指定(単位はpx)※positionより優先
17
+ * <dokodemo-ui x="100" y="200">...</dokodemo-ui>
18
+ */
19
+ class DokodemoUI extends HTMLElement {
20
+ static get observedAttributes() {
21
+ return ["closable"];
22
+ }
23
+ get closable() {
24
+ return this.hasAttribute("closable");
25
+ }
26
+ set closable(value) {
27
+ if (value) {
28
+ this.setAttribute("closable", "");
29
+ }
30
+ else {
31
+ this.removeAttribute("closable");
32
+ }
33
+ }
34
+ get position() {
35
+ return this.getAttribute("position");
36
+ }
37
+ set position(value) {
38
+ if (value) {
39
+ this.setAttribute("position", value);
40
+ }
41
+ else {
42
+ this.removeAttribute("position");
43
+ }
44
+ }
45
+ get padding() {
46
+ return parseInt(this.getAttribute("padding") || "20", 10);
47
+ }
48
+ set padding(value) {
49
+ this.setAttribute("padding", String(value));
50
+ }
51
+ get closeColor() {
52
+ return this.getAttribute("close-color") || "#ff5f57";
53
+ }
54
+ set closeColor(value) {
55
+ this.setAttribute("close-color", value);
56
+ }
57
+ get closeStyle() {
58
+ return this.getAttribute("close-style") || "circle";
59
+ }
60
+ set closeStyle(value) {
61
+ this.setAttribute("close-style", value);
62
+ }
63
+ get closePosition() {
64
+ return this.getAttribute("close-position") || "inside";
65
+ }
66
+ set closePosition(value) {
67
+ this.setAttribute("close-position", value);
68
+ }
69
+ get resizable() {
70
+ return this.hasAttribute("resizable");
71
+ }
72
+ set resizable(value) {
73
+ if (value) {
74
+ this.setAttribute("resizable", "");
75
+ }
76
+ else {
77
+ this.removeAttribute("resizable");
78
+ }
79
+ }
80
+ get x() {
81
+ const attr = this.getAttribute("x");
82
+ return attr ? parseInt(attr, 10) : null;
83
+ }
84
+ set x(value) {
85
+ if (value !== null) {
86
+ this.setAttribute("x", String(value));
87
+ this.style.left = `${value}px`;
88
+ }
89
+ else {
90
+ this.removeAttribute("x");
91
+ }
92
+ }
93
+ get y() {
94
+ const attr = this.getAttribute("y");
95
+ return attr ? parseInt(attr, 10) : null;
96
+ }
97
+ set y(value) {
98
+ if (value !== null) {
99
+ this.setAttribute("y", String(value));
100
+ this.style.top = `${value}px`;
101
+ }
102
+ else {
103
+ this.removeAttribute("y");
104
+ }
105
+ }
106
+ constructor() {
107
+ super();
108
+ this.isDragging = false;
109
+ this.isResizing = false;
110
+ this.hasMoved = false;
111
+ this.dragOffsetX = 0;
112
+ this.dragOffsetY = 0;
113
+ this.resizeStartX = 0;
114
+ this.resizeStartY = 0;
115
+ this.resizeStartWidth = 0;
116
+ this.resizeStartHeight = 0;
117
+ this.positionInitialized = false;
118
+ this.iframePointerEvents = new Map();
119
+ this.edgeSize = 8; // 端からの距離(px)
120
+ this.onMouseMoveForCursor = (e) => {
121
+ if (this.isDragging || this.isResizing)
122
+ return;
123
+ if (this.isNearEdge(e.clientX, e.clientY)) {
124
+ this.style.cursor = "grab";
125
+ }
126
+ else {
127
+ this.style.cursor = "";
128
+ }
129
+ };
130
+ this.onMouseLeaveForCursor = () => {
131
+ if (!this.isDragging) {
132
+ this.style.cursor = "";
133
+ }
134
+ };
135
+ this.onClickCapture = (e) => {
136
+ // 閉じるボタンのクリックは常に許可
137
+ const path = e.composedPath();
138
+ const isCloseButton = path.some((el) => el instanceof HTMLElement && el.classList.contains("close-button"));
139
+ if (isCloseButton)
140
+ return;
141
+ if (this.hasMoved) {
142
+ e.stopPropagation();
143
+ e.preventDefault();
144
+ this.hasMoved = false;
145
+ }
146
+ };
147
+ this.onMouseDown = (e) => {
148
+ // 内部の入力要素などはドラッグを開始しない
149
+ if (this.shouldIgnoreEvent(e))
150
+ return;
151
+ // 端からのみドラッグ可能
152
+ const path = e.composedPath();
153
+ const isEdgeOverlay = path.some((el) => el instanceof HTMLElement && el.classList.contains("edge-overlay"));
154
+ if (!isEdgeOverlay && !this.isNearEdge(e.clientX, e.clientY))
155
+ return;
156
+ e.preventDefault();
157
+ this.startDrag(e.clientX, e.clientY);
158
+ document.addEventListener("mousemove", this.onMouseMove);
159
+ document.addEventListener("mouseup", this.onMouseUp);
160
+ };
161
+ this.onTouchStart = (e) => {
162
+ if (this.shouldIgnoreEvent(e))
163
+ return;
164
+ const touch = e.touches[0];
165
+ // 端からのみドラッグ可能
166
+ const path = e.composedPath();
167
+ const isEdgeOverlay = path.some((el) => el instanceof HTMLElement && el.classList.contains("edge-overlay"));
168
+ if (!isEdgeOverlay && !this.isNearEdge(touch.clientX, touch.clientY))
169
+ return;
170
+ this.startDrag(touch.clientX, touch.clientY);
171
+ document.addEventListener("touchmove", this.onTouchMove, { passive: false });
172
+ document.addEventListener("touchend", this.onTouchEnd);
173
+ };
174
+ this.onMouseMove = (e) => {
175
+ if (!this.isDragging)
176
+ return;
177
+ e.preventDefault();
178
+ this.updatePosition(e.clientX, e.clientY);
179
+ };
180
+ this.onTouchMove = (e) => {
181
+ if (!this.isDragging)
182
+ return;
183
+ e.preventDefault();
184
+ const touch = e.touches[0];
185
+ this.updatePosition(touch.clientX, touch.clientY);
186
+ };
187
+ this.onMouseUp = () => {
188
+ this.endDrag();
189
+ document.removeEventListener("mousemove", this.onMouseMove);
190
+ document.removeEventListener("mouseup", this.onMouseUp);
191
+ };
192
+ this.onTouchEnd = () => {
193
+ this.endDrag();
194
+ document.removeEventListener("touchmove", this.onTouchMove);
195
+ document.removeEventListener("touchend", this.onTouchEnd);
196
+ };
197
+ this.onResizeMouseDown = (e) => {
198
+ e.preventDefault();
199
+ e.stopPropagation();
200
+ this.startResize(e.clientX, e.clientY);
201
+ document.addEventListener("mousemove", this.onResizeMouseMove);
202
+ document.addEventListener("mouseup", this.onResizeMouseUp);
203
+ };
204
+ this.onResizeTouchStart = (e) => {
205
+ e.preventDefault();
206
+ e.stopPropagation();
207
+ const touch = e.touches[0];
208
+ this.startResize(touch.clientX, touch.clientY);
209
+ document.addEventListener("touchmove", this.onResizeTouchMove, { passive: false });
210
+ document.addEventListener("touchend", this.onResizeTouchEnd);
211
+ };
212
+ this.onResizeMouseMove = (e) => {
213
+ if (!this.isResizing)
214
+ return;
215
+ e.preventDefault();
216
+ this.updateSize(e.clientX, e.clientY);
217
+ };
218
+ this.onResizeTouchMove = (e) => {
219
+ if (!this.isResizing)
220
+ return;
221
+ e.preventDefault();
222
+ const touch = e.touches[0];
223
+ this.updateSize(touch.clientX, touch.clientY);
224
+ };
225
+ this.onResizeMouseUp = () => {
226
+ this.endResize();
227
+ document.removeEventListener("mousemove", this.onResizeMouseMove);
228
+ document.removeEventListener("mouseup", this.onResizeMouseUp);
229
+ };
230
+ this.onResizeTouchEnd = () => {
231
+ this.endResize();
232
+ document.removeEventListener("touchmove", this.onResizeTouchMove);
233
+ document.removeEventListener("touchend", this.onResizeTouchEnd);
234
+ };
235
+ this.attachShadow({ mode: "open" });
236
+ }
237
+ connectedCallback() {
238
+ this.render();
239
+ this.setupDrag();
240
+ this.setupCloseButton();
241
+ this.setupResize();
242
+ }
243
+ attributeChangedCallback() {
244
+ this.render();
245
+ this.setupCloseButton();
246
+ }
247
+ render() {
248
+ if (!this.shadowRoot)
249
+ return;
250
+ this.shadowRoot.innerHTML = `
251
+ <style>
252
+ :host {
253
+ position: fixed;
254
+ user-select: none;
255
+ z-index: 9999;
256
+ }
257
+ :host(.dragging) {
258
+ cursor: grabbing !important;
259
+ }
260
+ :host([hidden]) {
261
+ display: none !important;
262
+ }
263
+ .container {
264
+ display: inline-block;
265
+ position: relative;
266
+ }
267
+ :host([resizable]) {
268
+ display: block;
269
+ }
270
+ :host([resizable]) .container {
271
+ width: 100%;
272
+ height: 100%;
273
+ display: block;
274
+ }
275
+ :host([resizable]) ::slotted(*) {
276
+ width: 100%;
277
+ height: 100%;
278
+ }
279
+ .resize-handle {
280
+ position: absolute;
281
+ bottom: 0;
282
+ right: 0;
283
+ width: 16px;
284
+ height: 16px;
285
+ cursor: nwse-resize;
286
+ background: linear-gradient(135deg, transparent 50%, rgba(0,0,0,0.3) 50%);
287
+ border-radius: 0 0 4px 0;
288
+ }
289
+ .resize-handle:hover {
290
+ background: linear-gradient(135deg, transparent 50%, rgba(0,0,0,0.5) 50%);
291
+ }
292
+ .edge-overlay {
293
+ position: absolute;
294
+ pointer-events: auto;
295
+ cursor: grab;
296
+ }
297
+ .edge-overlay:active {
298
+ cursor: grabbing;
299
+ }
300
+ .edge-top {
301
+ top: 0;
302
+ left: 0;
303
+ right: 0;
304
+ height: 8px;
305
+ }
306
+ .edge-bottom {
307
+ bottom: 0;
308
+ left: 0;
309
+ right: 0;
310
+ height: 8px;
311
+ }
312
+ .edge-left {
313
+ top: 0;
314
+ bottom: 0;
315
+ left: 0;
316
+ width: 8px;
317
+ }
318
+ .edge-right {
319
+ top: 0;
320
+ bottom: 0;
321
+ right: 0;
322
+ width: 8px;
323
+ }
324
+ .close-button {
325
+ position: absolute;
326
+ top: -8px;
327
+ right: -8px;
328
+ border: none;
329
+ cursor: pointer;
330
+ display: flex;
331
+ align-items: center;
332
+ justify-content: center;
333
+ transition: transform 0.1s, opacity 0.1s;
334
+ }
335
+ .close-button.circle {
336
+ width: 20px;
337
+ height: 20px;
338
+ border-radius: 50%;
339
+ background: ${this.closeColor};
340
+ color: white;
341
+ font-size: 14px;
342
+ line-height: 1;
343
+ box-shadow: 0 2px 4px rgba(0,0,0,0.2);
344
+ }
345
+ .close-button.circle:hover {
346
+ filter: brightness(0.85);
347
+ transform: scale(1.1);
348
+ }
349
+ .close-button.simple {
350
+ width: 20px;
351
+ height: 20px;
352
+ top: 4px;
353
+ right: 4px;
354
+ background: transparent;
355
+ color: ${this.closeColor};
356
+ font-size: 18px;
357
+ font-weight: bold;
358
+ line-height: 1;
359
+ text-shadow: 0 1px 2px rgba(0,0,0,0.3);
360
+ pointer-events: auto;
361
+ border-radius: 4px;
362
+ }
363
+ .close-button.simple.outside {
364
+ top: -20px;
365
+ right: -20px;
366
+ }
367
+ .close-button.simple:hover {
368
+ opacity: 0.7;
369
+ transform: scale(1.1);
370
+ }
371
+ </style>
372
+ <div class="container">
373
+ <slot></slot>
374
+ <div class="edge-overlay edge-top"></div>
375
+ <div class="edge-overlay edge-bottom"></div>
376
+ <div class="edge-overlay edge-left"></div>
377
+ <div class="edge-overlay edge-right"></div>
378
+ ${this.closable ? `<button class="close-button ${this.closeStyle}${this.closePosition === 'outside' ? ' outside' : ''}" aria-label="閉じる">×</button>` : ''}
379
+ ${this.resizable ? '<div class="resize-handle"></div>' : ''}
380
+ </div>
381
+ `;
382
+ // 初期位置を設定
383
+ this.initializePosition();
384
+ }
385
+ initializePosition() {
386
+ if (this.positionInitialized)
387
+ return;
388
+ this.positionInitialized = true;
389
+ // x, y が指定されていればそれを使用
390
+ if (this.x !== null && this.y !== null) {
391
+ this.style.left = `${this.x}px`;
392
+ this.style.top = `${this.y}px`;
393
+ return;
394
+ }
395
+ // position が指定されていれば計算
396
+ if (this.position) {
397
+ // 要素のサイズを取得するために一度表示
398
+ requestAnimationFrame(() => {
399
+ this.applyPosition();
400
+ });
401
+ return;
402
+ }
403
+ // デフォルト位置
404
+ this.style.left = "100px";
405
+ this.style.top = "100px";
406
+ }
407
+ applyPosition() {
408
+ const padding = `${this.padding}px`;
409
+ // 全ての位置プロパティをリセット
410
+ this.style.left = "";
411
+ this.style.right = "";
412
+ this.style.top = "";
413
+ this.style.bottom = "";
414
+ switch (this.position) {
415
+ case "top-left":
416
+ this.style.left = padding;
417
+ this.style.top = padding;
418
+ break;
419
+ case "top-right":
420
+ this.style.right = padding;
421
+ this.style.top = padding;
422
+ break;
423
+ case "bottom-left":
424
+ this.style.left = padding;
425
+ this.style.bottom = padding;
426
+ break;
427
+ case "bottom-right":
428
+ this.style.right = padding;
429
+ this.style.bottom = padding;
430
+ break;
431
+ case "center":
432
+ // center は計算が必要なので left/top を使用
433
+ const rect = this.getBoundingClientRect();
434
+ const width = rect.width || 100;
435
+ const height = rect.height || 50;
436
+ this.style.left = `${(window.innerWidth - width) / 2}px`;
437
+ this.style.top = `${(window.innerHeight - height) / 2}px`;
438
+ break;
439
+ default:
440
+ this.style.left = padding;
441
+ this.style.top = padding;
442
+ }
443
+ }
444
+ setupDrag() {
445
+ this.addEventListener("mousedown", this.onMouseDown);
446
+ this.addEventListener("touchstart", this.onTouchStart, { passive: false });
447
+ this.addEventListener("mousemove", this.onMouseMoveForCursor);
448
+ this.addEventListener("mouseleave", this.onMouseLeaveForCursor);
449
+ // ドラッグ後のクリックを無効化
450
+ this.addEventListener("click", this.onClickCapture, true);
451
+ }
452
+ isNearEdge(clientX, clientY) {
453
+ const rect = this.getBoundingClientRect();
454
+ const x = clientX - rect.left;
455
+ const y = clientY - rect.top;
456
+ // 端からedgeSize px以内かチェック
457
+ const nearLeft = x < this.edgeSize;
458
+ const nearRight = x > rect.width - this.edgeSize;
459
+ const nearTop = y < this.edgeSize;
460
+ const nearBottom = y > rect.height - this.edgeSize;
461
+ return nearLeft || nearRight || nearTop || nearBottom;
462
+ }
463
+ shouldIgnoreEvent(e) {
464
+ // composedPath() でShadow DOM内の実際のターゲットを取得
465
+ const path = e.composedPath();
466
+ for (const el of path) {
467
+ if (!(el instanceof HTMLElement))
468
+ continue;
469
+ // input, textarea, select はドラッグ対象外
470
+ const ignoreTags = ["INPUT", "TEXTAREA", "SELECT"];
471
+ if (ignoreTags.includes(el.tagName))
472
+ return true;
473
+ // 閉じるボタンはドラッグ対象外
474
+ if (el.classList.contains("close-button"))
475
+ return true;
476
+ // リサイズハンドルはドラッグ対象外
477
+ if (el.classList.contains("resize-handle"))
478
+ return true;
479
+ }
480
+ return false;
481
+ }
482
+ setupCloseButton() {
483
+ if (!this.shadowRoot || !this.closable)
484
+ return;
485
+ const closeButton = this.shadowRoot.querySelector(".close-button");
486
+ if (closeButton) {
487
+ closeButton.addEventListener("click", (e) => {
488
+ e.stopPropagation();
489
+ e.preventDefault();
490
+ this.hidden = true;
491
+ this.dispatchEvent(new CustomEvent("close", { bubbles: true }));
492
+ });
493
+ }
494
+ }
495
+ show() {
496
+ this.hidden = false;
497
+ }
498
+ hide() {
499
+ this.hidden = true;
500
+ }
501
+ startDrag(clientX, clientY) {
502
+ this.isDragging = true;
503
+ this.hasMoved = false;
504
+ this.classList.add("dragging");
505
+ this.disableIframePointerEvents();
506
+ const rect = this.getBoundingClientRect();
507
+ this.dragOffsetX = clientX - rect.left;
508
+ this.dragOffsetY = clientY - rect.top;
509
+ }
510
+ disableIframePointerEvents() {
511
+ const iframes = this.querySelectorAll("iframe");
512
+ iframes.forEach((iframe) => {
513
+ // 元の値を保存
514
+ this.iframePointerEvents.set(iframe, iframe.style.pointerEvents);
515
+ iframe.style.pointerEvents = "none";
516
+ });
517
+ }
518
+ restoreIframePointerEvents() {
519
+ this.iframePointerEvents.forEach((originalValue, iframe) => {
520
+ iframe.style.pointerEvents = originalValue;
521
+ });
522
+ this.iframePointerEvents.clear();
523
+ }
524
+ updatePosition(clientX, clientY) {
525
+ this.hasMoved = true;
526
+ // ドラッグ中は left/top を使用(right/bottom をクリア)
527
+ this.style.right = "";
528
+ this.style.bottom = "";
529
+ const newX = clientX - this.dragOffsetX;
530
+ const newY = clientY - this.dragOffsetY;
531
+ // 画面外に出ないように制限
532
+ const maxX = window.innerWidth - this.offsetWidth;
533
+ const maxY = window.innerHeight - this.offsetHeight;
534
+ this.style.left = `${Math.max(0, Math.min(newX, maxX))}px`;
535
+ this.style.top = `${Math.max(0, Math.min(newY, maxY))}px`;
536
+ }
537
+ endDrag() {
538
+ this.isDragging = false;
539
+ this.classList.remove("dragging");
540
+ this.restoreIframePointerEvents();
541
+ }
542
+ setupResize() {
543
+ if (!this.shadowRoot || !this.resizable)
544
+ return;
545
+ const handle = this.shadowRoot.querySelector(".resize-handle");
546
+ if (!handle)
547
+ return;
548
+ handle.addEventListener("mousedown", this.onResizeMouseDown);
549
+ handle.addEventListener("touchstart", this.onResizeTouchStart, { passive: false });
550
+ }
551
+ startResize(clientX, clientY) {
552
+ this.isResizing = true;
553
+ this.resizeStartX = clientX;
554
+ this.resizeStartY = clientY;
555
+ this.resizeStartWidth = this.offsetWidth;
556
+ this.resizeStartHeight = this.offsetHeight;
557
+ this.disableIframePointerEvents();
558
+ }
559
+ updateSize(clientX, clientY) {
560
+ const deltaX = clientX - this.resizeStartX;
561
+ const deltaY = clientY - this.resizeStartY;
562
+ const newWidth = Math.max(100, this.resizeStartWidth + deltaX);
563
+ const newHeight = Math.max(80, this.resizeStartHeight + deltaY);
564
+ this.style.width = `${newWidth}px`;
565
+ this.style.height = `${newHeight}px`;
566
+ }
567
+ endResize() {
568
+ this.isResizing = false;
569
+ this.restoreIframePointerEvents();
570
+ }
571
+ disconnectedCallback() {
572
+ this.removeEventListener("mousedown", this.onMouseDown);
573
+ this.removeEventListener("touchstart", this.onTouchStart);
574
+ this.removeEventListener("mousemove", this.onMouseMoveForCursor);
575
+ this.removeEventListener("mouseleave", this.onMouseLeaveForCursor);
576
+ }
577
+ }
578
+ customElements.define("dokodemo-ui", DokodemoUI);
579
+ export { DokodemoUI };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "dokodemo-ui",
3
+ "version": "1.0.0",
4
+ "description": "ドラッグで自由に移動できるWeb Component",
5
+ "main": "dokodemo-ui.js",
6
+ "types": "dokodemo-ui.d.ts",
7
+ "type": "module",
8
+ "files": [
9
+ "dokodemo-ui.js",
10
+ "dokodemo-ui.d.ts"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "dev": "tsc --watch",
15
+ "test": "playwright test",
16
+ "test:ui": "playwright test --ui",
17
+ "test:headed": "playwright test --headed",
18
+ "prepublishOnly": "npm run build && npm test"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/tamoco-mocomoco/dokodemo-ui.git"
23
+ },
24
+ "keywords": [
25
+ "web-component",
26
+ "draggable",
27
+ "ui",
28
+ "custom-element",
29
+ "floating",
30
+ "movable"
31
+ ],
32
+ "author": "tamoco-mocomoco",
33
+ "license": "MIT",
34
+ "bugs": {
35
+ "url": "https://github.com/tamoco-mocomoco/dokodemo-ui/issues"
36
+ },
37
+ "homepage": "https://github.com/tamoco-mocomoco/dokodemo-ui#readme",
38
+ "devDependencies": {
39
+ "@playwright/test": "^1.57.0",
40
+ "serve": "^14.2.5",
41
+ "typescript": "^5.3.0"
42
+ }
43
+ }