|
| 1 | +import React from 'react'; |
| 2 | +import { HiOutlineEye, HiOutlineEyeOff } from 'react-icons/hi'; |
| 3 | +import { HiOutlineLink } from 'react-icons/hi'; |
| 4 | + |
| 5 | +const ExerciseWithSolution = (props) => { |
| 6 | + const childrenWithoutP5Solution = props.children.filter( |
| 7 | + (child) => !child.props?.className?.includes('p5-solution'), |
| 8 | + ); |
| 9 | + const solutions = childrenWithoutP5Solution.filter((child) => { |
| 10 | + return child.props?.className?.includes('solution'); |
| 11 | + }); |
| 12 | + |
| 13 | + const [isAnswerVisible, setIsAnswerVisible] = React.useState(false); |
| 14 | + |
| 15 | + const toggleAnswerHiddenStatus = () => { |
| 16 | + setIsAnswerVisible((lastState) => !lastState); |
| 17 | + }; |
| 18 | + |
| 19 | + return ( |
| 20 | + <div |
| 21 | + className={`callout relative ${isAnswerVisible ? 'is-answer-visible' : ''}`} |
| 22 | + > |
| 23 | + {/* bottons group on the top right */} |
| 24 | + <div className="absolute right-0 top-0 hidden sm:flex"> |
| 25 | + {solutions.length && ( |
| 26 | + <button |
| 27 | + className="flex items-center rounded px-2.5 py-1.5 text-xs font-semibold hover:bg-gray-300" |
| 28 | + onClick={toggleAnswerHiddenStatus} |
| 29 | + > |
| 30 | + {isAnswerVisible ? ( |
| 31 | + <> |
| 32 | + <HiOutlineEyeOff className="h-4 w-4" /> |
| 33 | + <span className="ml-1">Hide Answer</span> |
| 34 | + </> |
| 35 | + ) : ( |
| 36 | + <> |
| 37 | + <HiOutlineEye className="h-4 w-4" /> |
| 38 | + <span className="ml-1">Reveal Answer</span> |
| 39 | + </> |
| 40 | + )} |
| 41 | + </button> |
| 42 | + )} |
| 43 | + |
| 44 | + {props.p5EditorUrl && ( |
| 45 | + <a |
| 46 | + className="flex items-center rounded px-2.5 py-1.5 text-xs font-semibold text-gray-700 no-underline hover:bg-gray-300" |
| 47 | + target="_blank" |
| 48 | + rel="noopener noreferrer" |
| 49 | + href={props.p5EditorUrl} |
| 50 | + > |
| 51 | + <HiOutlineLink className="h-4 w-4" /> |
| 52 | + <span className="ml-1">Suggested Answer</span> |
| 53 | + </a> |
| 54 | + )} |
| 55 | + </div> |
| 56 | + |
| 57 | + {childrenWithoutP5Solution} |
| 58 | + </div> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +export default ExerciseWithSolution; |
0 commit comments