gab-social/app/javascript/gabsocial/components/character_counter.js

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-02-14 00:40:04 +00:00
import { length } from 'stringz'
export default class CharacterCounter extends PureComponent {
2019-07-02 08:10:25 +01:00
static propTypes = {
text: PropTypes.string.isRequired,
max: PropTypes.number.isRequired,
2020-02-28 15:20:47 +00:00
small: PropTypes.bool,
2020-02-14 00:40:04 +00:00
}
2019-07-02 08:10:25 +01:00
render () {
2020-02-28 15:20:47 +00:00
const { text, max, small } = this.props
const actualRadius = small ? '10' : '16'
const radius = small ? 8 : 12
2020-02-14 00:40:04 +00:00
const circumference = 2 * Math.PI * radius
2020-02-28 15:20:47 +00:00
const diff = length(text) / max
2020-02-14 00:40:04 +00:00
const dashoffset = circumference * (1 - diff)
2020-01-27 19:46:42 +00:00
return (
2020-03-11 23:56:18 +00:00
<div className={[_s.default, _s.mr10, _s.justifyContentCenter, _s.alignItemsCenter].join(' ')}>
2020-02-28 15:20:47 +00:00
<svg width={actualRadius * 2} height={actualRadius * 2} viewBox={`0 0 ${actualRadius * 2} ${actualRadius * 2}`}>
<circle fill='none' cx={actualRadius} cy={actualRadius} r={radius} fill="none" stroke="#e6e6e6" strokeWidth="2" />
2020-02-14 00:40:04 +00:00
<circle style={{
// transform: 'rotate(-90deg)',
strokeDashoffset: dashoffset,
strokeDasharray: circumference,
}}
fill='none'
2020-02-28 15:20:47 +00:00
cx={actualRadius}
cy={actualRadius}
r={radius}
2020-02-14 00:40:04 +00:00
strokeWidth="2"
strokeLinecap='round'
stroke='#21cf7a'
/>
</svg>
2020-01-27 19:46:42 +00:00
</div>
)
2019-07-02 08:10:25 +01:00
}
}