TutorialTray.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2017 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react'
  19. import PropTypes from 'prop-types'
  20. import I18n from 'i18n!new_user_tutorial'
  21. import plainStoreShape from 'jsx/shared/proptypes/plainStoreShape'
  22. import Tray from 'instructure-ui/lib/components/Tray'
  23. import Button from 'instructure-ui/lib/components/Button'
  24. import NewUserTutorialToggleButton from '../NewUserTutorialToggleButton'
  25. import ConfirmEndTutorialDialog from '../ConfirmEndTutorialDialog'
  26. class TutorialTray extends React.Component {
  27. static propTypes = {
  28. // Used as a label for the content (screenreader-only)
  29. label: PropTypes.string.isRequired,
  30. // The specific tray that will be wrapped, unusable without this.
  31. children: PropTypes.node.isRequired,
  32. // The store to control the status of everything
  33. store: PropTypes.shape(plainStoreShape).isRequired,
  34. // Should return an element that focus can be set to
  35. returnFocusToFunc: PropTypes.func.isRequired
  36. }
  37. constructor (props) {
  38. super(props);
  39. this.state = {
  40. ...props.store.getState(),
  41. endUserTutorialShown: false
  42. };
  43. }
  44. componentDidMount () {
  45. this.props.store.addChangeListener(this.handleStoreChange)
  46. }
  47. componentWillUnmount () {
  48. this.props.store.removeChangeListener(this.handleStoreChange)
  49. }
  50. handleStoreChange = () => {
  51. this.setState(this.props.store.getState());
  52. }
  53. handleToggleClick = () => {
  54. this.props.store.setState({
  55. isCollapsed: !this.state.isCollapsed
  56. });
  57. }
  58. handleEndTutorialClick = () => {
  59. this.setState({
  60. endUserTutorialShown: true
  61. });
  62. }
  63. closeEndTutorialDialog = () => {
  64. this.setState({
  65. endUserTutorialShown: false
  66. });
  67. if (this.endTutorialButton) {
  68. this.endTutorialButton.focus();
  69. }
  70. }
  71. handleEntering = () => {
  72. this.toggleButton.focus()
  73. }
  74. handleExiting = () => {
  75. this.props.returnFocusToFunc().focus();
  76. }
  77. render () {
  78. return (
  79. <Tray
  80. label={this.props.label}
  81. open={!this.state.isCollapsed}
  82. placement="end"
  83. zIndex="100"
  84. onExiting={this.handleExiting}
  85. onEntered={this.handleEntering}
  86. shouldContainFocus
  87. applicationElement={() => document.getElementById('application')}
  88. >
  89. <div className="NewUserTutorialTray">
  90. <div className="NewUserTutorialTray__ButtonContainer">
  91. <NewUserTutorialToggleButton
  92. ref={(c) => { this.toggleButton = c; }}
  93. onClick={this.handleToggleClick}
  94. store={this.props.store}
  95. />
  96. </div>
  97. {this.props.children}
  98. <div className="NewUserTutorialTray__EndTutorialContainer">
  99. <Button
  100. onClick={this.handleEndTutorialClick}
  101. ref={(c) => { this.endTutorialButton = c; }}
  102. >
  103. {I18n.t('End Tutorial')}
  104. </Button>
  105. </div>
  106. <ConfirmEndTutorialDialog
  107. isOpen={this.state.endUserTutorialShown}
  108. handleRequestClose={this.closeEndTutorialDialog}
  109. />
  110. </div>
  111. </Tray>
  112. );
  113. }
  114. }
  115. export default TutorialTray