Skip to content

Instantly share code, notes, and snippets.

@hand-dot
Created February 13, 2024 07:31
Show Gist options
  • Select an option

  • Save hand-dot/f2aa2c4bfdc9acd8472fcb8b1f9b38d7 to your computer and use it in GitHub Desktop.

Select an option

Save hand-dot/f2aa2c4bfdc9acd8472fcb8b1f9b38d7 to your computer and use it in GitHub Desktop.
custom-element-fileuploader
class FileUploader extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
const input = document.createElement('input');
input.type = 'file';
this.shadowRoot.appendChild(input);
// inputのchangeイベントリスナーを設定
input.addEventListener('change', (event) => {
// 選択されたファイルを取得
const file = event.target.files[0];
this.selectedFile = file;
// カスタムイベントを作成して発火
const customEvent = new CustomEvent('file-changed', {
detail: { file }, // イベントに関連付けられたデータ
bubbles: true, // イベントがバブリングするかどうか
composed: true // イベントがShadow DOMの境界を越えるかどうか
});
this.dispatchEvent(customEvent);
});
}
}
customElements.define('file-uploader', FileUploader);
$w.onReady(function () {
$w("#customElement1").on('file-changed', (event) => {
console.log('file: ', event.detail.file)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment