The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

138 lines
4.5 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  4. import Overlay from 'react-overlays/lib/Overlay';
  5. import Motion from '../../ui/util/optional_motion';
  6. import spring from 'react-motion/lib/spring';
  7. import { searchEnabled } from '../../../initial_state';
  8. import Icon from 'mastodon/components/icon';
  9. const messages = defineMessages({
  10. placeholder: { id: 'search.placeholder', defaultMessage: 'Search' },
  11. });
  12. class SearchPopout extends React.PureComponent {
  13. static propTypes = {
  14. style: PropTypes.object,
  15. };
  16. render () {
  17. const { style } = this.props;
  18. const extraInformation = searchEnabled ? <FormattedMessage id='search_popout.tips.full_text' defaultMessage='Simple text returns statuses you have written, favourited, boosted, or have been mentioned in, as well as matching usernames, display names, and hashtags.' /> : <FormattedMessage id='search_popout.tips.text' defaultMessage='Simple text returns matching display names, usernames and hashtags' />;
  19. return (
  20. <div style={{ ...style, position: 'absolute', width: 285, zIndex: 2 }}>
  21. <Motion defaultStyle={{ opacity: 0, scaleX: 0.85, scaleY: 0.75 }} style={{ opacity: spring(1, { damping: 35, stiffness: 400 }), scaleX: spring(1, { damping: 35, stiffness: 400 }), scaleY: spring(1, { damping: 35, stiffness: 400 }) }}>
  22. {({ opacity, scaleX, scaleY }) => (
  23. <div className='search-popout' style={{ opacity: opacity, transform: `scale(${scaleX}, ${scaleY})` }}>
  24. <h4><FormattedMessage id='search_popout.search_format' defaultMessage='Advanced search format' /></h4>
  25. <ul>
  26. <li><em>#example</em> <FormattedMessage id='search_popout.tips.hashtag' defaultMessage='hashtag' /></li>
  27. <li><em>@username@domain</em> <FormattedMessage id='search_popout.tips.user' defaultMessage='user' /></li>
  28. <li><em>URL</em> <FormattedMessage id='search_popout.tips.user' defaultMessage='user' /></li>
  29. <li><em>URL</em> <FormattedMessage id='search_popout.tips.status' defaultMessage='status' /></li>
  30. </ul>
  31. {extraInformation}
  32. </div>
  33. )}
  34. </Motion>
  35. </div>
  36. );
  37. }
  38. }
  39. export default @injectIntl
  40. class Search extends React.PureComponent {
  41. static contextTypes = {
  42. router: PropTypes.object.isRequired,
  43. };
  44. static propTypes = {
  45. value: PropTypes.string.isRequired,
  46. submitted: PropTypes.bool,
  47. onChange: PropTypes.func.isRequired,
  48. onSubmit: PropTypes.func.isRequired,
  49. onClear: PropTypes.func.isRequired,
  50. onShow: PropTypes.func.isRequired,
  51. openInRoute: PropTypes.bool,
  52. intl: PropTypes.object.isRequired,
  53. };
  54. state = {
  55. expanded: false,
  56. };
  57. handleChange = (e) => {
  58. this.props.onChange(e.target.value);
  59. }
  60. handleClear = (e) => {
  61. e.preventDefault();
  62. if (this.props.value.length > 0 || this.props.submitted) {
  63. this.props.onClear();
  64. }
  65. }
  66. handleKeyUp = (e) => {
  67. if (e.key === 'Enter') {
  68. e.preventDefault();
  69. this.props.onSubmit();
  70. if (this.props.openInRoute) {
  71. this.context.router.history.push('/search');
  72. }
  73. } else if (e.key === 'Escape') {
  74. document.querySelector('.ui').parentElement.focus();
  75. }
  76. }
  77. handleFocus = () => {
  78. this.setState({ expanded: true });
  79. this.props.onShow();
  80. }
  81. handleBlur = () => {
  82. this.setState({ expanded: false });
  83. }
  84. render () {
  85. const { intl, value, submitted } = this.props;
  86. const { expanded } = this.state;
  87. const hasValue = value.length > 0 || submitted;
  88. return (
  89. <div className='search'>
  90. <label>
  91. <span style={{ display: 'none' }}>{intl.formatMessage(messages.placeholder)}</span>
  92. <input
  93. className='search__input'
  94. type='text'
  95. placeholder={intl.formatMessage(messages.placeholder)}
  96. value={value}
  97. onChange={this.handleChange}
  98. onKeyUp={this.handleKeyUp}
  99. onFocus={this.handleFocus}
  100. onBlur={this.handleBlur}
  101. />
  102. </label>
  103. <div role='button' tabIndex='0' className='search__icon' onClick={this.handleClear}>
  104. <Icon id='search' className={hasValue ? '' : 'active'} />
  105. <Icon id='times-circle' className={hasValue ? 'active' : ''} aria-label={intl.formatMessage(messages.placeholder)} />
  106. </div>
  107. <Overlay show={expanded && !hasValue} placement='bottom' target={this}>
  108. <SearchPopout />
  109. </Overlay>
  110. </div>
  111. );
  112. }
  113. }