{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/web-components-concepts","result":{"data":{"markdownRemark":{"id":"e41f235c-593e-53d6-a5b4-c882c807d834","html":"<p>最近 Web Components という言葉を耳にする機会が増えました。</p>\n<p>この記事には、Web Components の概要と、React や Vue.js などのコンポーネント指向のフレームワークとの違い、iframe との違いを簡単にまとめました。</p>\n<h2 id=\"web-components-の概要\" style=\"position:relative;\"><a href=\"#web-components-%E3%81%AE%E6%A6%82%E8%A6%81\" aria-label=\"web components の概要 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Web Components の概要</h2>\n<h3 id=\"コンポーネント指向とは\" style=\"position:relative;\"><a href=\"#%E3%82%B3%E3%83%B3%E3%83%9D%E3%83%BC%E3%83%8D%E3%83%B3%E3%83%88%E6%8C%87%E5%90%91%E3%81%A8%E3%81%AF\" aria-label=\"コンポーネント指向とは permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>コンポーネント指向とは</h3>\n<p>まず前提となるコンポーネント指向について簡単に説明します。</p>\n<p>最近の Web フロントエンド開発では、React や Vue.js といったコンポーネント指向のフレームワークが使われる場合が多いです。</p>\n<p>コンポーネント指向のフレームワークでは、<code class=\"language-text\">&lt;my-button&gt;</code> といった独自の HTML タグを作成します。</p>\n<p><code class=\"language-text\">&lt;my-button&gt;</code> などのコンポーネントは、構造・見た目・挙動 (HTML・CSS・JavaScript) を持っており、各所でコンポーネントを再利用することで、HTML・CSS・JavaScript を一箇所で定義することを可能にします。</p>\n<p>また、通常の HTML タグと同様、<code class=\"language-text\">&lt;my-button onclick=&quot;xxx&quot;&gt;</code> のようにして、外部から与えた属性で挙動などを変えられるようにすることも多いです。</p>\n<p>コンポーネント指向により、画面を構成する要素を部品化するアトミック・デザインを実装に落とし込むことも可能になります。</p>\n<h3 id=\"web-components-とは\" style=\"position:relative;\"><a href=\"#web-components-%E3%81%A8%E3%81%AF\" aria-label=\"web components とは permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Web Components とは</h3>\n<p><a href=\"https://developer.mozilla.org/ja/docs/Web/Web_Components\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Web Components | MDN</a> によると、</p>\n<blockquote>\n<p>Web Components は、再利用可能なカスタム要素を作成し、ウェブアプリの中で利用するための、一連のテクノロジーです。コードの他の部分から独立した、カプセル化された機能を使って実現します。 </p>\n</blockquote>\n<p>とのことです。</p>\n<p>ざっくり言うと、<code class=\"language-text\">&lt;my-button&gt;</code> のようなコンポーネントの定義をフレームワークに依存せずに可能にするのが「Web Components」です。</p>\n<p>この記事ではコンポーネントの具体的な作り方についてはふれませんが、Web Components でコンポーネントを作成する方法は、React などでコンポーネントを作るのとかなり近いです。</p>\n<p>機能としても React や Vue.js でコンポーネントを作成するときと似たものが提供されています。\n例えば HTML 属性 (props) の定義であったり、テンプレートの記述方法、slot (children) によって外部から子要素を与える方法などが定められています。</p>\n<p>要するに、React や Vue.js などのフレームワークがそれぞれ独自にしていたコンポーネント定義方法を標準化した技術が「Web Components」です。</p>\n<p>ちなみに、Web Components のコンポーネントは、<a href=\"https://www.webcomponents.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">webcomponents.org</a> で公開されていたりします。</p>\n<h2 id=\"web-components-と-react-や-vuejs-の違い\" style=\"position:relative;\"><a href=\"#web-components-%E3%81%A8-react-%E3%82%84-vuejs-%E3%81%AE%E9%81%95%E3%81%84\" aria-label=\"web components と react や vuejs の違い permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Web Components と React や Vue.js の違い</h2>\n<p>上記のようにコンポーネントの定義方法を標準化した技術が Web Components なのですが、逆に言えば、Web Components はコンポーネント化の技術でしかなく、React や Vue.js が持つ機能の全てを持っている訳ではありません。</p>\n<p>例えば、データバインディングであったり、Redux や Vuex による状態管理などは提供されていません。</p>\n<p>Web Components では、データバインディングは Polymer などのライブラリで実現したり、状態管理には Redux を使ったりすることになります。</p>\n<h2 id=\"iframe-との違い\" style=\"position:relative;\"><a href=\"#iframe-%E3%81%A8%E3%81%AE%E9%81%95%E3%81%84\" aria-label=\"iframe との違い permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>iframe との違い</h2>\n<p>最後に、iframe との違いを説明します。\nWeb Components について調べるまで、私は iframe との違いは何だろうと思っていました。</p>\n<p>Web Components と iframe の違いを調べると、「iframe は要素外から要素内に干渉できないが、Web Components はできる」などの説明がされる場合があります。</p>\n<p>これは挙動の違いとして間違ってはいないのですが、そもそも Web Components と iframe はどちらか選ぶといったものではありません。\n根本的に使い道が違うのです。</p>\n<p>Web Component はボタンなどの要素をコンポーネント化するための技術です。\n一方で、iframe は他の Web ページを自分のページ内に組み込む技術です。</p>\n<p>ですので、Web Components と iframe でどちらが優れているといった話ではなく、用途によって使い分けることになります。</p>\n<h2 id=\"参考\" style=\"position:relative;\"><a href=\"#%E5%8F%82%E8%80%83\" aria-label=\"参考 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>参考</h2>\n<p>書籍</p>\n<ul>\n<li><a href=\"https://amzn.to/2BhdunF\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">やさしく学べるWeb Components (技術の泉シリーズ（NextPublishing）)</a></li>\n</ul>\n<p>Web</p>\n<ul>\n<li><a href=\"https://www.apps-gcp.com/start-webcomponents-with-polymer/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Web ComponentsはじめるならPolymerで</a></li>\n<li><a href=\"https://tech.speee.jp/entry/2016/08/05/154253\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">サイト埋め込みコンテンツをWebComponentsで作る</a></li>\n<li><a href=\"https://liginc.co.jp/web/html-css/html/58267\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Web開発者に革命をもたらす！「Web Components」超入門</a></li>\n<li><a href=\"https://qiita.com/japboy/items/ca8f2a73a2089f2e026d\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Web Components FTW</a></li>\n</ul>","fields":{"slug":"/posts/web-components-concepts","tagSlugs":["/tag/web/"],"autoRecommendPosts":["udemy-web-application-development-with-javascript","books-about-how-web-and-web-applications-works","web-security-books","javascript-next-step-books"]},"frontmatter":{"date":"2020-07-19T02:33:22.719Z","description":"最近 Web Components という言葉を耳にする機会が増えました。\nこの記事には、Web Components の概要と、React や Vue.js などのコンポーネント指向のフレームワークとの違い、iframe との違いを簡単にまとめました。","tags":["web"],"title":"Web Components の概要と、React や Vue.js との違い、iframe との違い","socialImage":null,"recommendPosts":["https://www.kanzennirikaisita.com/posts/software-developer-books-index"]}}},"pageContext":{"slug":"/posts/web-components-concepts"}},"staticQueryHashes":["251939775","3942705351","401334301"]}