{"id":1280,"date":"2023-03-18T13:19:40","date_gmt":"2023-03-18T05:19:40","guid":{"rendered":"https:\/\/fdream.net\/blog\/?p=1280"},"modified":"2023-04-06T15:23:05","modified_gmt":"2023-04-06T07:23:05","slug":"%e5%9c%a8react%e4%b8%ad%e5%88%86%e7%a6%bbshadow-dom%e6%a0%b7%e5%bc%8f%e5%8f%8a%e4%bd%bf%e7%94%a8sass","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/1280","title":{"rendered":"\u5728React\u4e2d\u5206\u79bbShadow DOM\u6837\u5f0f\u53ca\u4f7f\u7528Sass"},"content":{"rendered":"\n<p id=\"u1373a1d4\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Web_Components\/Using_shadow_DOM\" target=\"_blank\" rel=\"noreferrer noopener\">Shadow DOM<\/a>\u5df2\u7ecf\u6bd4\u8f83\u6210\u719f\u4e86\uff0c\u57fa\u672c\u4e0a\u6240\u6709\u7684\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u652f\u6301\u4e86\uff0c\u53ef\u4ee5\u53c2\u8003<a href=\"https:\/\/caniuse.com\/?search=shadow%20dom\" target=\"_blank\" rel=\"noreferrer noopener\">Can I Use<\/a>\u3002\u5728React\u4e2d\u4f7f\u7528shadow DOM\u4e5f\u4e0d\u662f\u4ec0\u4e48\u65b0\u9c9c\u4e8b\uff0c\u4e5f\u6709\u4e00\u4e9b\u5173\u4e8eshadow DOM\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f46\u662f\u5728CSS\u65b9\u9762\uff0c\u51e0\u4e4e\u90fd\u662f\u4f7f\u7528\u4e86<a href=\"https:\/\/styled-components.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">styled components<\/a>\u7684\u65b9\u5f0f\u3002\u6211\u4e2a\u4eba\u4e0d\u592a\u559c\u6b22\u8fd9\u79cdcss\u548cjs\u6df7\u5728\u4e00\u8d77\u65b9\u5f0f\uff0c\u800c\u4e14\u8fd9\u79cd\u65b9\u5f0f\u4e0b\u4e5f\u6ca1\u6cd5\u4f7f\u7528scss\u4e86\u3002<\/p>\n\n\n\n<p id=\"u5cc5875f\">\u5f00\u59cb\u770b\u4e86\u4e0bstyle-laoder\u7684\u6587\u6863\uff0c\u91cc\u9762\u6709\u4e2a<a href=\"https:\/\/webpack.js.org\/loaders\/style-loader\/#insert\" target=\"_blank\" rel=\"noreferrer noopener\">\u9009\u9879insert<\/a>\uff0c\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2afunction\uff0c\u8fd9\u6837\u4f60\u53ef\u4ee5\u5728\u91cc\u9762\u8fdb\u884c\u4f60\u60f3\u8981\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u627e\u5230shadow DOM\u7684shadow root\uff0c\u7136\u540e\u628a\u4f60\u7684style\u63d2\u5165\u8fdb\u53bb:<\/p>\n\n\n\n<pre id=\"jmDxs\" class=\"wp-block-code\"><code>rules: &#91;\n  \/\/ Other webpack modules\n  ...\n  {\n    test: \/\\.css$\/,\n    use: &#91;\n      {\n        loader: require.resolve('style-loader'),\n        options: {\n          insertInto: function () {\n            return document.getElementById('root').shadowRoot;\n          },\n        },\n      },\n      \/\/ Other css loaders (if any)\n      ...\n    ],\n  },\n  ...\n],<\/code><\/pre>\n\n\n\n<p id=\"u2769b3b0\">\u5f88\u9057\u61be\uff0c\u5982\u679c\u4f60\u7684shadow root\u662f\u52a8\u6001\u6dfb\u52a0\u7684\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u884c\u4e0d\u901a\u3002<\/p>\n\n\n\n<p id=\"u26b40299\">\u53e6\u5916\u56de\u5230\u539f\u70b9\uff0c\u65e2\u7136style\u53ef\u4ee5\u52a8\u6001\u6ce8\u5165\uff0c\u90a3\u4e48\u6211\u4eec\u662f\u4e0d\u662f\u4e5f\u53ef\u4ee5\u7528\u7c7b\u4f3ccss module\u7684\u65b9\u5f0f\uff0c\u628ascss\u5f53\u505a\u5b57\u7b26\u4e32\u5f15\u5165\u8fdb\u6765\uff0c\u7136\u540eappend\u5230shadow root\u4e0a\uff1f<a href=\"https:\/\/github.com\/m4thieulavoie\/sass-to-string\" target=\"_blank\" rel=\"noreferrer noopener\">sass-to-string<\/a>\u6b63\u597d\u53ef\u4ee5\u505a\u5230\uff0c\u4e0d\u5982\u8bd5\u8bd5\u770b\u3002<\/p>\n\n\n\n<p id=\"u6cc8e64b\">\u9996\u5148\u914d\u7f6ewebpack\uff1a<\/p>\n\n\n\n<pre id=\"nD6TW\" class=\"wp-block-code\"><code>rules: &#91;\n  ...\n  {\n    test: \/\\.styles.scss$\/,\n    exclude: \/node_modules\/,\n    use: &#91;\n      'sass-to-string',\n      {\n        loader: 'sass-loader',\n        options: {\n          sassOptions: {\n            outputStyle: 'compressed'\n          }\n        }\n      }\n    ]\n  },\n  ...\n],<\/code><\/pre>\n\n\n\n<p id=\"u5d58ef5f\">\u7136\u540e\u521b\u5efa\u5355\u72ec\u7684popup.styles.scss\uff1a<\/p>\n\n\n\n<pre id=\"kuQMf\" class=\"wp-block-code\"><code>$img-width: 1.2rem;\n\n.popup {\n  position: absolute;\n  bottom: 20px;\n  left: 10px;\n  z-index: 3;\n}\n\nimg {\n  width: $img-width;\n}<\/code><\/pre>\n\n\n\n<p id=\"ubd81f78a\">\u518d\u5728\u5165\u53e3\u6587\u4ef6\u4e2d\u5f15\u5165css\uff1a<\/p>\n\n\n\n<pre id=\"c0vde\" class=\"wp-block-code\"><code>import styleText from '.\/popup-info.scss';\n\nconst style = document.createElement('style');\nstyle.appendChild(document.createTextNode(styleText));\n\nconst shadowRoot = this.attachShadow({ mode: 'open' });\nshadowRoot.appendChild(style);\n\/\/ ...<\/code><\/pre>\n\n\n\n<p id=\"uc2b1b0ab\">\u81f3\u6b64\uff0c\u57fa\u672c\u89e3\u51b3\u4e86style\u72ec\u7acb\u5e76\u4e14\u80fd\u591f\u4f7f\u7528sass\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shadow DOM\u5df2\u7ecf\u6bd4\u8f83\u6210\u719f\u4e86\uff0c\u57fa\u672c\u4e0a\u6240\u6709\u7684\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u652f\u6301\u4e86\uff0c\u53ef\u4ee5\u53c2\u8003Can I Use\u3002\u5728React\u4e2d\u4f7f\u7528shadow DOM\u4e5f\u4e0d\u662f\u4ec0\u4e48\u65b0\u9c9c\u4e8b\uff0c\u4e5f\u6709\u4e00\u4e9b\u5173\u4e8eshadow DOM\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f46\u662f\u5728CSS\u65b9\u9762\uff0c\u51e0\u4e4e\u90fd\u662f\u4f7f\u7528\u4e86styled components\u7684\u65b9\u5f0f\u3002\u6211\u4e2a\u4eba\u4e0d\u592a\u559c\u6b22\u8fd9\u79cdcss\u548cjs\u6df7\u5728\u4e00\u8d77\u65b9\u5f0f\uff0c\u800c\u4e14\u8fd9\u79cd\u65b9\u5f0f\u4e0b\u4e5f\u6ca1\u6cd5\u4f7f\u7528scss\u4e86\u3002<\/p>\n<p>\u5f00\u59cb\u770b\u4e86\u4e0bstyle-laoder\u7684\u6587\u6863\uff0c\u91cc\u9762\u6709\u4e2a\u9009\u9879insert\uff0c\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2afunction\uff0c\u8fd9\u6837\u4f60\u53ef\u4ee5\u5728\u91cc\u9762\u8fdb\u884c\u4f60\u60f3\u8981\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u627e\u5230shadow DOM\u7684shadow root\uff0c\u7136\u540e\u628a\u4f60\u7684style\u63d2\u5165\u8fdb\u53bb<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[105,373,384,383],"class_list":["post-1280","post","type-post","status-publish","format-standard","hentry","category-coding","tag-JS","tag-reactjs","tag-shadow-dom","tag-web-components"],"views":1952,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=1280"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}