[
  {
    "path": ".editorconfig",
    "content": "[*.js]\nindent_style = space\nindent_size = 2"
  },
  {
    "path": ".github/workflows/main.yml",
    "content": "name: Node.js CI\n\non:\n  push:\n    branches: [master]\n  pull_request:\n    branches: [master]\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n\n    strategy:\n      matrix:\n        node-version: [16.x]\n\n    steps:\n      - uses: actions/checkout@v2\n      - name: Use Node.js ${{ matrix.node-version }}\n        uses: actions/setup-node@v2\n        with:\n          node-version: ${{ matrix.node-version }}\n      - run: npm ci\n      - run: npm run build --if-present\n      - run: npm test\n"
  },
  {
    "path": ".gitignore",
    "content": "# Logs\nlogs\n*.log\nnpm-debug.log*\n\n# Runtime data\npids\n*.pid\n*.seed\n\n# Directory for instrumented libs generated by jscoverage/JSCover\nlib-cov\n\n# Coverage directory used by tools like istanbul\ncoverage\n\n# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)\n.grunt\n\n# node-waf configuration\n.lock-wscript\n\n# Compiled binary addons (http://nodejs.org/api/addons.html)\nbuild/Release\n\n# Dependency directory\nnode_modules\n\n# Optional npm cache directory\n.npm\n\n# Optional REPL history\n.node_repl_history\n.vscode\ndist\n.rts2_cache_*"
  },
  {
    "path": ".prettierrc",
    "content": "{\n  \"arrowParens\": \"always\",\n  \"singleQuote\": true,\n  \"trailingComma\": \"none\",\n  \"semi\": false\n}\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2016 Jiri Spac\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"react-tweet-embed\",\n  \"version\": \"2.0.0\",\n  \"description\": \"react component that takes tweet id in props and renders tweet embed, nothing more\",\n  \"main\": \"dist/tweet-embed.js\",\n  \"types\": \"dist/tweet-embed.d.ts\",\n  \"module\": \"dist/tweet-embed.es.js\",\n  \"unpkg\": \"dist/tweet-embed.umd.js\",\n  \"source\": \"src/tweet-embed.tsx\",\n  \"scripts\": {\n    \"test\": \"ava\",\n    \"testu\": \"ava -u\",\n    \"build\": \"microbundle --jsx React.createElement\",\n    \"dev\": \"microbundle watch\",\n    \"prepublish\": \"npm test && npm run build\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/capaj/react-tweet-embed.git\"\n  },\n  \"author\": \"capajj@gmail.com\",\n  \"license\": \"MIT\",\n  \"bugs\": {\n    \"url\": \"https://github.com/capaj/react-tweet-embed/issues\"\n  },\n  \"homepage\": \"https://github.com/capaj/react-tweet-embed#readme\",\n  \"peerDependencies\": {\n    \"react\": \">=17\"\n  },\n  \"devDependencies\": {\n    \"@types/prop-types\": \"^15.7.4\",\n    \"@types/react\": \"^17.0.39\",\n    \"ava\": \"^3.15.0\",\n    \"enzyme\": \"^3.11.0\",\n    \"husky\": \"^7.0.4\",\n    \"jsdom\": \"^17.0.0\",\n    \"microbundle\": \"^0.14.2\",\n    \"prettier\": \"^2.5.1\",\n    \"pretty-quick\": \"^3.1.3\",\n    \"prop-types\": \"^15.8.1\",\n    \"react\": \"^17.0.2\",\n    \"rimraf\": \"^3.0.2\",\n    \"ts-node\": \"^10.5.0\",\n    \"typescript\": \"^4.5.5\"\n  },\n  \"files\": [\n    \"dist\"\n  ],\n  \"ava\": {\n    \"files\": [\n      \"**/*.spec.tsx\"\n    ],\n    \"extensions\": [\n      \"ts\",\n      \"tsx\"\n    ],\n    \"require\": [\n      \"ts-node/register\"\n    ]\n  },\n  \"husky\": {\n    \"hooks\": {\n      \"pre-commit\": \"pretty-quick --staged\"\n    }\n  }\n}\n"
  },
  {
    "path": "readme.md",
    "content": "# react-tweet-embed\n\n## Install\n\n```\nnpm i react-tweet-embed\n```\n\n## Quickstart [![Edit react-tweet-embed](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/74rn3r9k0?fontsize=14)\n\n```tsx\nimport React from 'react'\nimport TweetEmbed from 'react-tweet-embed'\n\nexport const App = () => {\n  return <TweetEmbed tweetId=\"692527862369357824\" />\n}\n```\n\nYou don't have to put `//platform.twitter.com/widgets.js` script in your index.html as this lib will put it there if `twttr` is not found on `window`.\n\n## Props\n\n### `placeholder`\n\nText to be shown while the tweet is loading:\n```html\n<TweetEmbed id='1186523464712146944' placeholder='Sending this tweet through space via Starlink satellite 🛰'/>\n```\n\n### `options`\n\nKey-value pairs from the [embedded tweet parameter reference](https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/guides/embedded-tweet-parameter-reference):\n\n```html\n<TweetEmbed id='783943172057694208' options={{cards: 'hidden' }}/>\n<TweetEmbed id='771763270273294336' options={{theme: 'dark' }}/>\n\n```\n"
  },
  {
    "path": "src/tweet-embed.spec.tsx",
    "content": "import test from 'ava'\nimport TweetEmbed from './tweet-embed'\nimport React from 'react'\n\ntest('renders', (t) => {\n  t.snapshot(<TweetEmbed tweetId=\"692527862369357824\" className=\"tweet\" />)\n})\n\ntest.cb('calls twttr api', (t) => {\n  global['window'] = {\n    // @ts-expect-error\n    twttr: {\n      widgets: {\n        createTweetEmbed: (...args) => {\n          t.snapshot(args)\n          t.end()\n          return Promise.resolve()\n        }\n      }\n    }\n  }\n  // @ts-expect-error\n  global['window'].twttr.ready = () => Promise.resolve(global['window'].twttr)\n\n  const comp = new TweetEmbed({ tweetId: 'tweet_id', options: { myOption: 1 } })\n  comp.componentDidMount()\n})\n"
  },
  {
    "path": "src/tweet-embed.spec.tsx.md",
    "content": "# Snapshot report for `src/tweet-embed.spec.tsx`\n\nThe actual snapshot is saved in `tweet-embed.spec.tsx.snap`.\n\nGenerated by [AVA](https://avajs.dev).\n\n## renders\n\n> Snapshot 1\n\n    <TweetEmbed⍟\n      className=\"tweet\"\n      options={{}}\n      protocol=\"https:\"\n      tweetId=\"692527862369357824\"\n    />\n\n## calls twttr api\n\n> Snapshot 1\n\n    [\n      'tweet_id',\n      undefined,\n      {\n        myOption: 1,\n      },\n    ]\n"
  },
  {
    "path": "src/tweet-embed.tsx",
    "content": "import React from 'react'\nimport PropTypes from 'prop-types'\n\nconst callbacks = []\n\nfunction addScript(src: string, cb: () => any) {\n  if (callbacks.length === 0) {\n    callbacks.push(cb)\n    var s = document.createElement('script')\n    s.setAttribute('src', src)\n    s.addEventListener('load', () => callbacks.forEach((cb) => cb()), false)\n    document.body.appendChild(s)\n  } else {\n    callbacks.push(cb)\n  }\n}\n\ninterface ITweetEmbedProps {\n  tweetId: string\n  options?: object\n  placeholder?: string | React.ReactNode\n  protocol?: string\n  onTweetLoadSuccess?: (twitterWidgetElement: HTMLElement) => any\n  onTweetLoadError?: (err: Error) => any\n  className?: string\n}\n\ninterface ITweetEmbedState {\n  isLoading: boolean\n}\n\nclass TweetEmbed extends React.Component<ITweetEmbedProps> {\n  _div?: HTMLDivElement\n  static propTypes = {\n    tweetId: PropTypes.string,\n    options: PropTypes.object,\n    placeholder: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),\n    protocol: PropTypes.string,\n    onTweetLoadSuccess: PropTypes.func,\n    onTweetLoadError: PropTypes.func,\n    className: PropTypes.string\n  }\n\n  static defaultProps = {\n    protocol: 'https:',\n    options: {},\n    className: null\n  }\n\n  state: ITweetEmbedState = {\n    isLoading: true\n  }\n  loadTweetForProps(props: ITweetEmbedProps) {\n    const renderTweet = () => {\n      const twttr = window['twttr']\n      twttr.ready().then(({ widgets }) => {\n        // Clear previously rendered tweet before rendering the updated tweet id\n        if (this._div) {\n          this._div.innerHTML = ''\n        }\n\n        const { options, onTweetLoadSuccess, onTweetLoadError } = props\n        widgets\n          .createTweetEmbed(this.props.tweetId, this._div, options)\n          .then((twitterWidgetElement) => {\n            this.setState({\n              isLoading: false\n            })\n            onTweetLoadSuccess && onTweetLoadSuccess(twitterWidgetElement)\n          })\n          .catch(onTweetLoadError)\n      })\n    }\n\n    const twttr = window['twttr']\n    if (!(twttr && twttr.ready)) {\n      const isLocal = window.location.protocol.indexOf('file') >= 0\n      const protocol = isLocal ? this.props.protocol : ''\n\n      addScript(`${protocol}//platform.twitter.com/widgets.js`, renderTweet)\n    } else {\n      renderTweet()\n    }\n  }\n\n  componentDidMount() {\n    this.loadTweetForProps(this.props)\n  }\n\n  shouldComponentUpdate(\n    nextProps: ITweetEmbedProps,\n    nextState: ITweetEmbedState\n  ) {\n    return (\n      this.props.tweetId !== nextProps.tweetId ||\n      this.props.className !== nextProps.className\n    )\n  }\n\n  componentWillUpdate(nextProps, nextState) {\n    if (this.props.tweetId !== nextProps.tweetId) {\n      this.loadTweetForProps(nextProps)\n    }\n  }\n\n  render() {\n    const { props, state } = this\n    const {\n      tweetId,\n      onTweetLoadError,\n      onTweetLoadSuccess,\n      options,\n      children,\n      placeholder,\n      protocol,\n      ...restProps\n    } = props\n    return (\n      <div\n        {...restProps}\n        ref={(c) => {\n          this._div = c\n        }}\n      >\n        {state.isLoading && props.placeholder}\n      </div>\n    )\n  }\n}\n\nexport default TweetEmbed\n"
  },
  {
    "path": "tsconfig.json",
    "content": "{\n  \"compilerOptions\": {\n    \"esModuleInterop\": true,\n    \"jsx\": \"react\"\n  }\n}\n"
  }
]