@pie-lib/correct-answer-toggle 4.0.2 → 4.0.3-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.jsx DELETED
@@ -1,177 +0,0 @@
1
- import { styled } from '@mui/material/styles';
2
- import { CSSTransition } from 'react-transition-group';
3
- import { CorrectResponse } from '@pie-lib/icons';
4
- import { color, Readable } from '@pie-lib/render-ui';
5
- import Expander from './expander';
6
- import React from 'react';
7
- import PropTypes from 'prop-types';
8
- import Translator from '@pie-lib/translator';
9
-
10
- const { translator } = Translator;
11
-
12
- const noTouch = {
13
- '-webkit-touchCcallout': 'none',
14
- '-webkit-user-select': 'none',
15
- '-khtml-user-select': 'none',
16
- '-moz-user-select': 'none',
17
- '-ms-user-select': 'none',
18
- 'user-select': 'none',
19
- };
20
-
21
- const StyledRoot = styled('div')(() => ({
22
- width: '100%',
23
- cursor: 'pointer',
24
- }));
25
-
26
- const StyledContent = styled('div')(() => ({
27
- margin: '0 auto',
28
- textAlign: 'center',
29
- display: 'flex',
30
- }));
31
-
32
- const StyledLabel = styled('div')(() => ({
33
- width: 'fit-content',
34
- minWidth: '140px',
35
- alignSelf: 'center',
36
- verticalAlign: 'middle',
37
- color: `var(--correct-answer-toggle-label-color, ${color.text()})`,
38
- fontWeight: 'normal',
39
- ...noTouch,
40
- }));
41
-
42
- const StyledIcon = styled('div')(() => ({
43
- position: 'absolute',
44
- width: '25px',
45
- '&.enter': {
46
- opacity: '0',
47
- },
48
- '&.enter-active': {
49
- opacity: '1',
50
- transition: 'opacity 0.3s ease-in',
51
- },
52
- '&.exit': {
53
- opacity: '1',
54
- },
55
- '&.exit-active': {
56
- opacity: '0',
57
- transition: 'opacity 0.3s ease-in',
58
- },
59
- }));
60
-
61
- const StyledIconHolder = styled('div')(() => ({
62
- width: '25px',
63
- marginRight: '5px',
64
- display: 'flex',
65
- alignItems: 'center',
66
- }));
67
-
68
- /**
69
- * We export the raw unstyled class for testability. For public use please use the default export.
70
- */
71
- export class CorrectAnswerToggle extends React.Component {
72
- static propTypes = {
73
- onToggle: PropTypes.func,
74
- toggled: PropTypes.bool,
75
- show: PropTypes.bool,
76
- hideMessage: PropTypes.string,
77
- showMessage: PropTypes.string,
78
- className: PropTypes.string,
79
- language: PropTypes.string,
80
- };
81
-
82
- static defaultProps = {
83
- showMessage: 'Show correct answer',
84
- hideMessage: 'Hide correct answer',
85
- show: false,
86
- toggled: false,
87
- };
88
-
89
- constructor(props) {
90
- super(props);
91
- this.state = {
92
- show: props.show,
93
- };
94
- this.openIconRef = React.createRef();
95
- this.closedIconRef = React.createRef();
96
-
97
- CorrectAnswerToggle.defaultProps = {
98
- ...CorrectAnswerToggle.defaultProps,
99
- showMessage: translator.t('common:showCorrectAnswer', { lng: props.language }),
100
- hideMessage: translator.t('common:hideCorrectAnswer', { lng: props.language }),
101
- };
102
- }
103
-
104
- onClick() {
105
- this.props.onToggle(!this.props.toggled);
106
- }
107
- onTouch(event) {
108
- event.preventDefault(); // Prevents the default action (click event)
109
- this.props.onToggle(!this.props.toggled);
110
- }
111
-
112
- UNSAFE_componentWillReceiveProps(nextProps) {
113
- this.setState({
114
- show: nextProps.show,
115
- });
116
-
117
- if (nextProps.language !== this.props?.language) {
118
- CorrectAnswerToggle.defaultProps = {
119
- ...CorrectAnswerToggle.defaultProps,
120
- showMessage: translator.t('common:showCorrectAnswer', { lng: nextProps.language }),
121
- hideMessage: translator.t('common:hideCorrectAnswer', { lng: nextProps.language }),
122
- };
123
- }
124
- }
125
-
126
- render() {
127
- const { className, toggled, hideMessage, showMessage } = this.props;
128
-
129
- return (
130
- <StyledRoot className={className}>
131
- <Expander show={this.state.show}>
132
- <StyledContent onClick={this.onClick.bind(this)} onTouchEnd={this.onTouch.bind(this)}>
133
- <StyledIconHolder>
134
- <CSSTransition
135
- nodeRef={this.openIconRef}
136
- timeout={400}
137
- in={toggled}
138
- exit={!toggled}
139
- classNames={{
140
- enter: 'enter',
141
- enterActive: 'enter-active',
142
- exit: 'exit',
143
- exitActive: 'exit-active',
144
- }}
145
- >
146
- <StyledIcon ref={this.openIconRef}>
147
- <CorrectResponse open={toggled} key="correct-open" />
148
- </StyledIcon>
149
- </CSSTransition>
150
- <CSSTransition
151
- nodeRef={this.closedIconRef}
152
- timeout={5000}
153
- in={!toggled}
154
- exit={toggled}
155
- classNames={{
156
- enter: 'enter',
157
- enterActive: 'enter-active',
158
- exit: 'exit',
159
- exitActive: 'exit-active',
160
- }}
161
- >
162
- <StyledIcon ref={this.closedIconRef}>
163
- <CorrectResponse open={toggled} key="correct-closed" />
164
- </StyledIcon>
165
- </CSSTransition>
166
- </StyledIconHolder>
167
- <Readable false>
168
- <StyledLabel aria-hidden={!this.state.show}>{toggled ? hideMessage : showMessage}</StyledLabel>
169
- </Readable>
170
- </StyledContent>
171
- </Expander>
172
- </StyledRoot>
173
- );
174
- }
175
- }
176
-
177
- export default CorrectAnswerToggle;