ComposeForm: yank circular svg character counter from Gab

profile-avatar-switcher
Alex Gleason 2021-09-22 16:05:17 -05:00
rodzic 15ba91e96d
commit f635494629
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
5 zmienionych plików z 112 dodań i 24 usunięć

Wyświetl plik

@ -0,0 +1,62 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
class ProgressCircle extends React.PureComponent {
static defaultProps = {
radius: 12,
stroke: 4,
}
render() {
const { progress, radius, stroke, title } = this.props;
const progressStroke = stroke + 0.5;
const actualRadius = radius + progressStroke;
const circumference = 2 * Math.PI * radius;
const dashoffset = circumference * (1 - Math.min(progress, 1));
return (
<div className={classNames('progress-circle', { 'progress-circle--over': progress > 1 })} title={title}>
<svg
width={actualRadius * 2}
height={actualRadius * 2}
viewBox={`0 0 ${actualRadius * 2} ${actualRadius * 2}`}
>
<circle
className='progress-circle__circle'
cx={actualRadius}
cy={actualRadius}
r={radius}
fill='none'
strokeWidth={stroke}
/>
<circle
className={classNames('progress-circle__progress')}
style={{
strokeDashoffset: dashoffset,
strokeDasharray: circumference,
}}
cx={actualRadius}
cy={actualRadius}
r={radius}
fill='none'
strokeWidth={progressStroke}
strokeLinecap='round'
/>
</svg>
</div>
);
}
}
ProgressCircle.propTypes = {
progress: PropTypes.number.isRequired,
radius: PropTypes.number,
stroke: PropTypes.number,
title: PropTypes.text,
};
export default ProgressCircle;

Wyświetl plik

@ -1,25 +1,42 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { injectIntl, defineMessages } from 'react-intl';
import { length } from 'stringz'; import { length } from 'stringz';
import ProgressCircle from 'soapbox/components/progress_circle';
export default class CharacterCounter extends React.PureComponent { const messages = defineMessages({
title: { id: 'compose.character_counter.title', defaultMessage: 'Used {chars} out of {maxChars} characters' },
});
static propTypes = { /**
text: PropTypes.string.isRequired, * Renders a character counter
max: PropTypes.number.isRequired, * @param {string} props.text - text to use to measure
}; * @param {number} props.max - max text allowed
*/
checkRemainingText(diff) { class CharacterCounter extends React.PureComponent {
if (diff < 0) {
return <span className='character-counter character-counter--over'>{diff}</span>;
}
return <span className='character-counter'>{diff}</span>;
}
render() { render() {
const diff = this.props.max - length(this.props.text); const { intl, text, max } = this.props;
return this.checkRemainingText(diff);
const textLength = length(text);
const progress = textLength / max;
return (
<ProgressCircle
title={intl.formatMessage(messages.title, { chars: textLength, maxChars: max })}
progress={progress}
radius={10}
stroke={4}
/>
);
} }
} }
CharacterCounter.propTypes = {
intl: PropTypes.object.isRequired,
text: PropTypes.string.isRequired,
max: PropTypes.number.isRequired,
};
export default injectIntl(CharacterCounter);

Wyświetl plik

@ -88,6 +88,7 @@
@import 'components/aliases'; @import 'components/aliases';
@import 'components/icon'; @import 'components/icon';
@import 'components/profile-stats'; @import 'components/profile-stats';
@import 'components/progress-circle';
// Holiday // Holiday
@import 'holiday/halloween'; @import 'holiday/halloween';

Wyświetl plik

@ -383,15 +383,6 @@
.character-counter__wrapper { .character-counter__wrapper {
align-self: center; align-self: center;
margin: 0 10px 0 auto; margin: 0 10px 0 auto;
.character-counter {
cursor: default;
font-family: var(--font-sans-serif), sans-serif;
font-size: 14px;
font-weight: 600;
color: var(--primary-text-color--faint);
&.character-counter--over { color: $warning-red; }
}
} }
} }

Wyświetl plik

@ -0,0 +1,17 @@
.progress-circle {
display: flex;
&__circle {
stroke: hsla(var(--primary-text-color_hsl), 0.2);
}
&__progress {
stroke: var(--brand-color);
}
&--over {
.progress-circle__progress {
stroke: $warning-red;
}
}
}