Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a Clipboard Button component #1743

Merged
merged 5 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

### Fixes

- 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 @@ -15,7 +17,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
60 changes: 60 additions & 0 deletions lib/components/clipboard-button/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import PropTypes from 'prop-types';
import ReactDom from 'react-dom';
import Clipboard from 'clipboard';

function ClipboardButton({ text, disabled }) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that we have this prop but we never set it. Should we just leave it out for now?

const buttonRef = React.useRef();

belcherj marked this conversation as resolved.
Show resolved Hide resolved
const textCallback = React.useRef();
const successCallback = React.useRef();

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

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

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

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

// create the `Clipboard` object on mount and destroy on unmount
React.useEffect(() => {
const buttonEl = ReactDom.findDOMNode(buttonRef.current);
belcherj marked this conversation as resolved.
Show resolved Hide resolved
const clipboard = new Clipboard(buttonEl, {
text: () => textCallback.current(),
});
clipboard.on('success', () => successCallback.current());

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

return (
<button
ref={buttonRef}
disabled={disabled}
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