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

feat(selectable): disallows text from being selectable #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import Avatar, { ConfigProvider } from 'react-avatar';
| `src` | *string* | | Fallback image to use |
| `style` | *object* | | Style that will be applied on the root element |
| `unstyled` | *bool* | false | Disable all styles |
| `selectable` | *bool* | false | Whether it is possible to select the initials text. |
| `onClick` | *function* | | Mouse click event |

### ConfigProvider
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export interface ReactAvatarProps {
* Disable all styles
*/
unstyled?: boolean;
/**
* Whether it is possible to select the initials text.
*/
selectable?: boolean;
/**
* Mouse click event
* @param {React.SyntheticEvent<any>} e
Expand Down
17 changes: 14 additions & 3 deletions src/components/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AvatarText extends React.PureComponent {
PropTypes.number,
PropTypes.string
]),
selectable: PropTypes.bool,
}

static defaultProps = {
Expand All @@ -45,7 +46,8 @@ class AvatarText extends React.PureComponent {
size: 100,
textSizeRatio: 3,
textMarginRatio: .15,
unstyled: false
unstyled: false,
selectable: false,
}

componentDidMount() {
Expand Down Expand Up @@ -122,7 +124,8 @@ class AvatarText extends React.PureComponent {
title,
name,
value,
avatar
avatar,
selectable
} = this.props;

const size = parseSize(this.props.size);
Expand All @@ -144,11 +147,19 @@ class AvatarText extends React.PureComponent {
height: '100%'
};

const unselectableStyle = {
WebkitUserSelect: 'none',
MsUserSelect: 'none',
userSelect: 'none',
};

const spanStyle = unstyled ? null : {
display: 'table-cell',
verticalAlign: 'middle',
fontSize: '100%',
whiteSpace: 'nowrap'
whiteSpace: 'nowrap',

...(!selectable && unselectableStyle)
};

// Ensure the text node is updated and scaled when any of these
Expand Down