Skip to content

Commit

Permalink
Adds a Clipboard Button component (#1743)
Browse files Browse the repository at this point in the history
The inline button component to copy the publish url was lacking an indication
that the text was copied. Additionally, the current setup was using a copy
method that can have an issue in some browsers. This commit switches to use of the
clipboard npm repo, moves code into a component, and adds an indication that
the text was copied.
  • Loading branch information
belcherj authored Dec 16, 2019
1 parent 86f0d3a commit 7d3bf61
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 30 deletions.
4 changes: 3 additions & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
### Fixes
- Makes settings scrollable on shorter smaller view ports [#1767](https://github.com/Automattic/simplenote-electron/pull/1767)

- Added indication that publish url has been copied [#1743](https://github.com/Automattic/simplenote-electron/pull/1743)

### Other Changes

- Updated dependencies [#1759](https://github.com/Automattic/simplenote-electron/pull/1759)
Expand All @@ -17,7 +19,7 @@

### Enhancements

- Added matching tags to the notes list on search [#1648](https://github.com/Automattic/simplenote-electron/pull/1648)
- Added matching tags to the notes list on search [#1648](https://github.com/Automattic/simplenote-electron/pull/1648)

### Fixes

Expand Down
53 changes: 53 additions & 0 deletions lib/components/clipboard-button/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import ReactDom from 'react-dom';
import Clipboard from 'clipboard';

function ClipboardButton({ text }) {
const buttonRef = useRef();
const textCallback = useRef();
const successCallback = useRef();

const onCopy = () => {
setCopied(true);
};

const [isCopied, setCopied] = useState(false);

// toggle the `isCopied` flag back to `false` after 4 seconds
useEffect(() => {
if (isCopied) {
const confirmationTimeout = setTimeout(() => setCopied(false), 4000);
return () => clearTimeout(confirmationTimeout);
}
}, [isCopied]);

// update the callbacks on rerenders that change `text` or `onCopy`
useEffect(() => {
textCallback.current = () => text;
successCallback.current = onCopy;
}, [text, onCopy]);

// create the `Clipboard` object on mount and destroy on unmount
useEffect(() => {
const clipboard = new Clipboard(buttonRef.current, {
text: () => textCallback.current(),
});
clipboard.on('success', () => successCallback.current());

return () => clipboard.destroy();
}, []);

return (
<button ref={buttonRef} type="button" className="button button-borderless">
{isCopied ? 'Copied!' : 'Copy'}
</button>
);
}

ClipboardButton.propTypes = {
disbaled: PropTypes.bool,
text: PropTypes.string,
};

export default ClipboardButton;
4 changes: 0 additions & 4 deletions lib/dialogs/settings/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
border-bottom: none;
}

.button-borderless {
padding-right: 0;
}

.settings-item-label {
flex: 1 1 auto;
align-items: center;
Expand Down
28 changes: 3 additions & 25 deletions lib/dialogs/share/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { includes, isEmpty } from 'lodash';
import MD5 from 'md5.js';

import analytics from '../../analytics';
import ClipboardButton from '../../components/clipboard-button';
import isEmailTag from '../../utils/is-email-tag';
import { updateNoteTags } from '../../state/domain/notes';
import Dialog from '../../dialog';
Expand Down Expand Up @@ -33,18 +34,6 @@ export class ShareDialog extends Component {
});
};

copyPublishURL = () => {
this.publishUrlElement.select();

try {
document.execCommand('copy');
} catch (err) {
return;
}

this.copyUrlElement.focus();
};

getPublishURL = url => (isEmpty(url) ? undefined : `http://simp.ly/p/${url}`);

onAddCollaborator = event => {
Expand Down Expand Up @@ -200,15 +189,7 @@ export class ShareDialog extends Component {
spellCheck={false}
/>
<div className="settings-item-control">
<button
ref={e => (this.copyUrlElement = e)}
disabled={!publishURL}
type="button"
className="button button-borderless"
onClick={this.copyPublishURL}
>
Copy
</button>
{publishURL && <ClipboardButton text={publishURL} />}
</div>
</div>
</div>
Expand All @@ -222,7 +203,4 @@ export class ShareDialog extends Component {
}
}

export default connect(
null,
{ updateNoteTags }
)(ShareDialog);
export default connect(null, { updateNoteTags })(ShareDialog);
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@automattic/color-studio": "2.2.0",
"@material-ui/core": "4.7.1",
"bottleneck": "2.19.5",
"clipboard": "2.0.4",
"cookie": "0.4.0",
"core-js": "3.4.7",
"date-fns": "2.8.1",
Expand Down

0 comments on commit 7d3bf61

Please sign in to comment.