{"id":208,"date":"2020-09-09T23:56:31","date_gmt":"2020-09-09T15:56:31","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=208"},"modified":"2020-09-30T17:57:07","modified_gmt":"2020-09-30T09:57:07","slug":"%e7%bb%84%e4%bb%b6%e7%bc%96%e7%a8%8b%e5%9b%9b","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2020\/09\/09\/%e7%bb%84%e4%bb%b6%e7%bc%96%e7%a8%8b%e5%9b%9b\/","title":{"rendered":"\u7ec4\u4ef6\u7f16\u7a0b\u56db"},"content":{"rendered":"<hr \/>\n<p>\u7ec4\u4ef6\u5c01\u88c5\u597d\u540e\uff0c\u4e0e\u5e38\u7528\u7684\u6846\u67b6\u6216\u5e93\u7c7b\u4f3c\u3002<\/p>\n<p>\u5f53\u5168\u5c40\u6837\u5f0f\u548c\u7ec4\u4ef6\u5185\u6837\u5f0f\u80fd\u591f\u5f88\u597d\u7684\u63a7\u5236\u540e\uff0c\u4e0b\u9762\u5bf9\u4e4b\u524d\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u8fdb\u884c\u5b8c\u5584\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">&lt;template id=\"new-template\"&gt;\n    &lt;div class=\"wrapper\"&gt;\n        &lt;div class=\"overlay\"&gt;&lt;\/div&gt;\n        &lt;div class=\"dialog\" role=\"dialog\" aria-labelledby=\"title\" aria-describedby=\"content\"&gt;\n            &lt;button class=\"close\" aria-label=\"Close\"&gt;\u00d7\ufe0f&lt;\/button&gt;\n            &lt;h1 id=\"title\"&gt;\n                &lt;slot name=\"heading\"&gt;&lt;\/slot&gt;\n            &lt;\/h1&gt;\n            &lt;div id=\"content\" class=\"content\"&gt;\n                &lt;slot&gt;&lt;\/slot&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;i-dialog template=\"new-template\"&gt;\n&lt;span slot=\"heading\"&gt;\u6211\u7684\u540d\u5b57\u5bf9\u4f60\u6709\u4f55\u610f\u4e49\uff1f&lt;\/span&gt;\n&lt;div&gt;\n    &lt;p&gt;\u60f3\u60f3\u5c0f\u65f6\u5019\uff0c\u7ecf\u5e38\u7ed9\u5c0f\u4f19\u4f34\u53d6\u201c\u5916\u53f7\u201d\u548c\u201c\u7ef0\u53f7\u201d\u53ef\u8c13\u4e00\u5927\u5feb\u4e8b\uff0c\u5e76\u4e14\u5927\u90e8\u5206\u7684\u5916\u53f7\u90fd\u4e0d\u662f\u90a3\u4e48\u6587\u96c5\uff0c\u755c\u751f\u7684\u5e2d\u4f4d\u53ef\u8c13\u5360\u4e86\u7edd\u5927\u591a\u6570\u3002\u53ef\u662f\u8fd9\u4e9b\u6e10\u6e10\u5730\u5728\u6211\u7684\u751f\u6d3b\u4e2d\u6162\u6162\u6d88\u5931\uff0c\u4e00\u70b9\u4e00\u70b9\u7684\uff0c\u4e0d\u7559\u75d5\u8ff9\u3002\n\n        \u4f60\u7684\u540d\u5b57\u5bf9\u6211\u6709\u4f55\u610f\u4e49\uff1f\u800c\u6211\u7684\u540d\u5b57\u5bf9\u4f60\u53c8\u6709\u4f55\u610f\u4e49\uff1f&lt;\/p&gt;\n\n    &lt;p&gt;\u53ef\u60b2\u7684\u662f\uff1a\u4eba\u4e0e\u4eba\u4e4b\u95f4\u7684\u7eaf\u7cb9\u7684\u5fe0\u80c6\u4fa0\u4e49\u8d8a\u6765\u8d8a\u591a\u5730\u53d8\u6210\u4e86\u73b0\u4ee3\u4eba\u7684\u6587\u5316\u7b26\u53f7\uff0c\u4e5f\u8bb8\uff0c\u6709\u4eba\u8bb2\uff0c\u65f6\u4ee3\u5728\u8fdb\u6b65\uff0c\u4eba\u4e0e\u4eba\u7684\u5173\u7cfb\u4e5f\u968f\u4e4b\u5728\u6162\u6162\u53d1\u5c55\u548c\u53d8\u5316\uff0c\u5e76\u975e\u8981\u62b1\u5b88\u7740\u4e00\u79cd\u81ea\u4ee5\u4e3a\u9ad8\u5c1a\u7684\u7cbe\u795e\u72b6\u6001\uff0c\u800c\u662f\u4e0d\u65ad\u5730\u53bb\u9002\u5e94\u793e\u4f1a\u7ed9\u4eba\u5e26\u6765\u7684\u65b0\u7684\u4e1c\u897f\u3002&lt;\/p&gt;\n\n    &lt;p&gt;&lt;a href=\"https:\/\/www.indeex.org\/cn\/2014\/02\/my-meaningful-name\/\" target=\"_blank\"&gt; indeex--\u300a\u98ce\u8046\u96c6\u300b&lt;\/a&gt;&lt;\/p&gt;\n&lt;\/div&gt;\n&lt;\/i-dialog&gt;\n\n&lt;button id=\"open-dialog\"&gt;\u6253\u5f00\u5f39\u7a97&lt;\/button&gt;\n<\/code><\/pre>\n<pre><code class=\"language-javascript line-numbers\">class IndeexDialog extends HTMLElement {\n    static get observedAttributes() {\n        return ['open', 'template'];\n    }\n\n    constructor() {\n        super();\n        this.attachShadow({\n            mode: 'open'\n        });\n        this.close = this.close.bind(this);\n    }\n\n    attributeChangedCallback(attrName, oldValue, newValue) {\n        if (newValue !== oldValue) {\n            switch (attrName) {\n                case 'open':\n                    this[attrName] = this.hasAttribute(attrName);\n                    break;\n                case 'template':\n                    this[attrName] = newValue;\n                    break;\n            }\n        }\n    }\n\n    connectedCallback() {\n        this.render();\n    }\n\n    disconnectedCallback() {\n        this.shadowRoot.querySelector('button').removeEventListener('click', this.close);\n        this.shadowRoot.querySelector('.overlay').removeEventListener('click', this.close);\n    }\n\n    render() {\n        const {\n            shadowRoot,\n            template\n        } = this;\n        const templateNode = document.getElementById(template);\n        if (templateNode) {\n            const content = document.importNode(templateNode.content, true);\n            shadowRoot.appendChild(content);\n        } else {\n            shadowRoot.innerHTML =\n                `&lt;style&gt;\n        .wrapper {\n          opacity: 0;\n          transition: visibility 0s, opacity 0.25s ease-in;\n        }\n        .wrapper:not(.open) {\n          visibility: hidden;\n        }\n        .wrapper.open {\n          align-items: center;\n          display: flex;\n          justify-content: center;\n          height: 100vh;\n          position: fixed;\n            top: 0;\n            left: 0;\n            right: 0;\n            bottom: 0;\n          opacity: 1;\n          visibility: visible;\n        }\n        .overlay {\n          background: rgba(0, 0, 0, 0.8);\n          height: 100%;\n          position: fixed;\n            top: 0;\n            right: 0;\n            bottom: 0;\n            left: 0;\n          width: 100%;\n        }\n        .dialog {\n          background: #ffffff;\n          max-width: 600px;\n          padding: 1rem;\n          position: fixed;\n        }\n        button {\n          all: unset;\n          cursor: pointer;\n          font-size: 1.25rem;\n          position: absolute;\n            top: 1rem;\n            right: 1rem;\n        }\n        button:hover {\n          color: #ca5335;\n        }\n      &lt;\/style&gt;\n      &lt;div class=\"wrapper\"&gt;\n      &lt;div class=\"overlay\"&gt;&lt;\/div&gt;\n        &lt;div class=\"dialog\" role=\"dialog\" aria-labelledby=\"title\" aria-describedby=\"content\"&gt;\n          &lt;button class=\"close\" aria-label=\"Close\"&gt;\u00d7\ufe0f&lt;\/button&gt;\n          &lt;h1 id=\"title\"&gt;&lt;slot name=\"heading\"&gt;&lt;\/slot&gt;&lt;\/h1&gt;\n          &lt;div id=\"content\" class=\"content\"&gt;\n            &lt;slot&gt;&lt;\/slot&gt;\n          &lt;\/div&gt;\n        &lt;\/div&gt;\n      &lt;\/div&gt;`;\n        }\n\n    shadowRoot.querySelector('button').addEventListener('click', this.close);\n    shadowRoot.querySelector('.overlay').addEventListener('click', this.close);\n    this.open = this.open;\n}\n\nget open() {\n    return this.hasAttribute('open');\n}\n\nget template() {\n    return this.getAttribute('template');\n}\n\n\nset template(template) {\n    if (template) {\n        this.setAttribute('template', template);\n    } else {\n        this.removeAttribute('template');\n    }\n    this.render();\n}\n\nset open(isOpen) {\n    const {\n        shadowRoot\n    } = this;\n    shadowRoot.querySelector('.wrapper').classList.toggle('open', isOpen);\n    shadowRoot.querySelector('.wrapper').setAttribute('aria-hidden', !isOpen);\n    if (isOpen) {\n        this._wasFocused = document.activeElement;\n        this.setAttribute('open', '');\n        document.addEventListener('keydown', this._watchEscape);\n        this.focus();\n        shadowRoot.querySelector('button').focus();\n    } else {\n        this._wasFocused &amp;&amp; this._wasFocused.focus &amp;&amp; this._wasFocused.focus();\n        this.removeAttribute('open');\n        document.removeEventListener('keydown', this._watchEscape);\n        this.close();\n    }\n}\n\n\nclose() {\n    if (this.open !== false) {\n        this.open = false;\n    }\n    const closeEvent = new CustomEvent('dialog-closed');\n    this.dispatchEvent(closeEvent);\n}\n\n_watchEscape(event) {\n    if (event.key === 'Escape') {\n        this.close();\n    }\n}\n}\n\ncustomElements.define('i-dialog', IndeexDialog);\n\nconst button = document.getElementById('open-dialog');\nbutton.addEventListener('click', () =&gt; {\ndocument.querySelector('i-dialog').open = true;\n})\n<\/code><\/pre>\n<p>\u7ec4\u4ef6\u5185\u6709\u5185\u90e8\u6837\u5f0f\uff0c\u5168\u5c40\u6837\u5f0f\u53ef\u4ee5\u8986\u76d6\u5185\u90e8\u6837\u5f0f<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/hungking.cc\/web-components\/dialog.html\">\u9884\u89c8<\/a><\/p>\n<p>\u5f53\u7136\uff0c\u4f9d\u7136\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u6837\u5f0f\u3002<\/p>\n<p>\u5bf9\u6bd4\u4e00\u4e0b\u5f53\u4e0b\u6d41\u884c\u6846\u67b6\u3002<\/p>\n<h3>Angular<\/h3>\n<p>Angular\u5728\u5904\u7406\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u65f6\uff0c\u78b0\u5230\u9884\u7f6e\u6216\u65e0\u6cd5\u89e3\u6790\u7684\u7ec4\u4ef6\u76f4\u63a5\u629b\u9519\uff0c\u5e76\u4e14\u9700\u8981\u9075\u5faa\u76f8\u5e94\u89c4\u5219\uff0c\u8be6\u89c1<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/angular.io\/api\/core\/CUSTOM_ELEMENTS_SCHEMA\">\u6587\u6863<\/a>\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular\/core';\n\n@NgModule({\n  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]\n})\nexport class MyModuleAllowsCustomElements {}\n\n\n\/\/...\n\n&lt;indeex-dialog [open]=\"isDialogOpen\" (dialog-closed)=\"dialogClosed($event)\"&gt;\n  &lt;span slot=\"heading\"&gt;A Header Text.....&lt;\/span&gt;\n  &lt;div&gt;\n    &lt;p&gt;Wahahaaaaa.....&lt;\/p&gt;\n  &lt;\/div&gt;\n&lt;\/indeex-dialog&gt;\n<\/code><\/pre>\n<h3>Vue<\/h3>\n<p>Vue\u5e93\u66f4\u9075\u5faa\u6807\u51c6\uff0c\u6240\u4ee5\u66f4\u5bb9\u6613\u914d\u5408\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">&lt;indeex-dialog v-bind:open=\"isDialogOpen\" v-on:dialog-closed=\"dialogClosed\"&gt;\n  &lt;span slot=\"heading\"&gt;A Header Text.....&lt;\/span&gt;\n  &lt;div&gt;\n    &lt;p&gt;Wahahaaaaa.....&lt;\/p&gt;\n  &lt;\/div&gt;\n&lt;\/indeex-dialog&gt;\n<\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u4e0d\u7ba1\u662fAngular\u8fd8\u662fVue\uff0c\u5bf9\u4e8e\u8868\u5355\u63a7\u4ef6\u4f1a\u590d\u6742\u5f88\u591a\uff0c\u4e0d\u5728\u8ba8\u8bba\u8303\u56f4\u5185\u3002<\/p>\n<h3>React<\/h3>\n<p>React\u5e93\u6bd4\u8f83\u590d\u6742\uff0c\u56e0\u4e3aReact\u91c7\u7528all in js\u7684\u65b9\u6848\uff0c\u7531\u4e8e\u4e8b\u4ef6\u88ab\u91cd\u5199\uff0c\u9700\u8981\u4f7f\u7528refs\uff0c\u4ee5\u4e0b\u662f\u6a21\u677f\u7ec4\u4ef6\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">import React, { Component, createRef } from 'react';\nimport PropTypes from 'prop-types';\n\nexport default class IndeexDialog extends Component {\n  constructor(props) {\n    super(props);\n    this.dialog = createRef();\n    this.onDialogClosed = this.onDialogClosed.bind(this);\n  }\n  componentDidMount() {\n    this.dialog.current.addEventListener('dialog-closed', this.onDialogClosed);\n  }\n\n  componentWillUnmount() {\n    this.dialog.current.removeEventListener('dialog-closed', this.onDialogClosed);\n  }\n\n  onDialogClosed(event) {\n    if (this.props.onDialogClosed) {\n      this.props.onDialogClosed(event);\n    }\n  }\n\n  render() {\n    const { children, onDialogClosed, ...props } = this.props;\n    return &lt;indeex-dialog {...props} ref={this.dialog}&gt;\n      {children}\n    &lt;\/indeex-dialog&gt;\n  }\n}\n\nIndeexDialog.propTypes = {\n  children: children: PropTypes.oneOfType([\n      PropTypes.arrayOf(PropTypes.node),\n      PropTypes.node\n  ]).isRequired,\n  onDialogClosed: PropTypes.func\n};\n<\/code><\/pre>\n<p>\u4ee5\u4e0b\u662fHooks\u7ec4\u4ef6\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">import React, { useRef, useEffect } from 'react';\nimport PropTypes from 'prop-types';\n\nexport default function IndeexDialog(props) {\n  const { children, onDialogClosed, ...restProps } = props;\n  const indeexDialog = useRef(null);\n\n  useEffect(() =&gt; {\n    onDialogClosed ? indeexDialog.current.addEventListener('dialog-closed', onDialogClosed) : null;\n    return () =&gt; {\n      onDialogClosed ? indeexDialog.current.removeEventListener('dialog-closed', onDialogClosed) : null;  \n    };\n  });\n\n  return &lt;indeex-dialog ref={indeexDialog} {...restProps}&gt;{children}&lt;\/indeex-dialog&gt;\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u5c31\u662f\u6b63\u5e38\u7684\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">import React, { useState } from 'react';\nimport IndeexDialog from '.\/IndeexDialog';\n\nexport default function MyComponent(props) {\n  const [open, setOpen] = useState(false);\n  return &lt;div&gt;\n    &lt;button onClick={() =&gt; setOpen(true)}&gt;Open dialog&lt;\/button&gt;\n    &lt;IndeexDialog open={open} onDialogClosed={() =&gt; setOpen(false)}&gt;\n      &lt;span slot=\"heading\"&gt;A Header Text.....&lt;\/span&gt;\n      &lt;div&gt;\n        &lt;p&gt;Wahahaaaaa.....&lt;\/p&gt;\n      &lt;\/div&gt;\n    &lt;\/IndeexDialog&gt;\n  &lt;\/div&gt;\n}\n<\/code><\/pre>\n<h3>Web\u5de5\u5177\u548c\u751f\u6001<\/h3>\n<p>\u4f7f\u7528Web\u76f8\u5173\u5de5\u5177\u3001\u6bd4\u5982Npm\u7b49\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u5c5e\u4e8e\u81ea\u5df1\u7684\u7ec4\u4ef6\u5e76\u53d1\u5e03\u4f9b\u4ed6\u4eba\u4f7f\u7528\u3002<\/p>\n<p>\u5f53\u7136\u4e5f\u6709\u5f88\u591a\u6210\u719f\u7684UI\u6846\u67b6\u53ef\u4ee5\u6311\u9009\uff0c\u6bd4\u5982Element\u3001Ant design\u7b49\uff0c\u4f46\u5fc5\u987b\u6709\u81ea\u6211\u521b\u5efa\u548c\u5b8c\u5584\u7ec4\u4ef6\u7684\u80fd\u529b\uff0c\u800c\u4e14\u4f7f\u7528\u539f\u751f\u5f00\u53d1\u81ea\u5df1\u7684\u7ec4\u4ef6\u624d\u662f\u6700\u597d\u7684\u9009\u62e9\u3002<\/p>\n<p>code enjoy!\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/indeex.club\/\">https:\/\/indeex.club<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec4\u4ef6\u5c01\u88c5\u597d\u540e\uff0c\u4e0e\u5e38\u7528\u7684\u6846\u67b6\u6216\u5e93\u7c7b\u4f3c\u3002 \u5f53\u5168\u5c40\u6837\u5f0f\u548c\u7ec4\u4ef6\u5185\u6837\u5f0f\u80fd\u591f\u5f88\u597d\u7684\u63a7\u5236\u540e\uff0c\u4e0b\u9762\u5bf9\u4e4b\u524d\u7684\u81ea\u5b9a\u4e49\u7ec4\u4ef6<a href=\"https:\/\/blog.indeex.club\/index.php\/2020\/09\/09\/%e7%bb%84%e4%bb%b6%e7%bc%96%e7%a8%8b%e5%9b%9b\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/208"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=208"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":209,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/208\/revisions\/209"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}