InviteButton.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import LinkIcon from 'material-icons-svg/components/baseline/Link';
  2. import React, { Component } from 'react';
  3. import styled from 'styled-components';
  4. import { TalkyButton } from '../styles/button';
  5. const Button = styled(TalkyButton)({
  6. gridArea: 'invite',
  7. backgroundColor: '#00b0eb',
  8. color: 'white',
  9. '&:hover': {
  10. backgroundColor: 'rgb(0, 158, 230)'
  11. },
  12. '& svg': {
  13. fill: 'white'
  14. }
  15. });
  16. interface State {
  17. copied: boolean;
  18. }
  19. export default class InviteButton extends Component<{}, State> {
  20. constructor(props: {}) {
  21. super(props);
  22. this.state = { copied: false };
  23. }
  24. public render() {
  25. return (
  26. <Button onClick={this.onClick}>
  27. <LinkIcon />
  28. <span>{this.state.copied ? 'Copied Link!' : 'Invite'}</span>
  29. </Button>
  30. );
  31. }
  32. private onClick = async () => {
  33. try {
  34. if ('clipboard' in navigator) {
  35. await (navigator as any).clipboard.writeText(window.location.href);
  36. } else {
  37. const el = document.createElement('textarea');
  38. el.style.fontSize = '12pt';
  39. // Reset box model
  40. el.style.border = '0';
  41. el.style.padding = '0';
  42. el.style.margin = '0';
  43. // Move element out of screen horizontally
  44. el.style.position = 'absolute';
  45. el.style.left = '-9999px';
  46. // Move element to the same position vertically
  47. const yPosition =
  48. window.pageYOffset || document.documentElement.scrollTop;
  49. el.style.top = `${yPosition}px`;
  50. document.body.appendChild(el);
  51. el.setAttribute('readonly', '');
  52. el.value = window.location.href;
  53. el.select();
  54. document.execCommand('copy');
  55. document.body.removeChild(el);
  56. }
  57. this.setState({ copied: true });
  58. } catch (err) {
  59. console.error(err);
  60. }
  61. };
  62. }