Skip to content

Commit

Permalink
resize content text inside exercise and completion status (#73)
Browse files Browse the repository at this point in the history
* resize content text inside exercise and completion status

* accept a className so styles can be added

* update snapshots

* bump version
  • Loading branch information
jivey authored Sep 20, 2024
1 parent 4db2d9e commit 37e3f8c
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 131 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openstax/assessment-components",
"version": "1.0.0",
"version": "1.1.0",
"license": "MIT",
"source": "./src/index.ts",
"types": "./dist/src/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { colors } from '../theme';
const StyledAnswerIndicator = styled.div<{ state: boolean }>`
color: ${props => props.state ? colors.answer.correct : colors.answer.incorrect};
text-transform: uppercase;
font-size: 1.1rem;
font-size: calc(1.1rem * var(--content-text-scale));
font-weight: bold;
`;

Expand Down
2 changes: 1 addition & 1 deletion src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ StepCardHeader.displayName = 'StepCardHeader';
const StepCardQuestion = styled.div<{ unpadded?: boolean }>`
.step-card-body {
${mixins.stepCardPadding()}
overflow: auto;
background: ${colors.card.body.background};
&.exercise-stimulus {
Expand Down
32 changes: 20 additions & 12 deletions src/components/CompletionStatus.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import styled from "styled-components";
import styled, { createGlobalStyle } from "styled-components";
import { InnerStepCard } from "./Card";
import Button from "./Button";

const GlobalStyle = createGlobalStyle`
:root {
--content-text-scale: 1;
}
`;

export interface CompletionStatusProps {
numberOfQuestions: number;
numberCompleted: number;
handleClick: () => void;
className?: string;
}

const CompletionStatusCard = styled(InnerStepCard)`
padding: 88px 72px;
font-size: 1.8rem;
line-height: 3rem;
font-size: calc(1.8rem * var(--content-text-scale));
line-height: calc(3rem * var(--content-text-scale));
display: block;
button {
Expand All @@ -25,27 +32,28 @@ const CompletionStatusCard = styled(InnerStepCard)`
`;

const CompletionHeader = styled.h2`
font-size: 2.4rem;
font-size: calc(2.4rem * var(--content-text-scale));
margin: 0;
`;

export const CompletionStatus = ({
numberOfQuestions, numberCompleted, handleClick
export const CompletionStatus = styled(({
numberOfQuestions, numberCompleted, handleClick, className
}: CompletionStatusProps) => {

const allCompleted = numberOfQuestions === numberCompleted;
const someCompleted = numberCompleted > 0;
const buttonText = allCompleted ? 'Next' : (
someCompleted ? 'Continue' : 'Start'
);

return (
<CompletionStatusCard>
return <>
<GlobalStyle />
<CompletionStatusCard className={className}>
<CompletionHeader>{allCompleted ? 'You are done.' : (someCompleted ? 'Quiz is partially complete.' : 'No questions have been answered.')}</CompletionHeader>
<p>{allCompleted ? 'Great job answering all the questions.' : (someCompleted ? `You've completed ${numberCompleted} of ${numberOfQuestions} questions.` : 'Begin working on the quiz.')}</p>
<Button data-test-id={`${buttonText.split(' ')[0].toLowerCase()}-btn`} onClick={()=> handleClick()}>
<Button data-test-id={`${buttonText.split(' ')[0].toLowerCase()}-btn`} onClick={() => handleClick()}>
{buttonText}
</Button>
</CompletionStatusCard>
)
};
</>
})``;
78 changes: 56 additions & 22 deletions src/components/Exercise.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState } from 'react';
import React, { useState } from 'react';
import { Exercise, ExerciseWithStepDataProps, ExerciseWithQuestionStatesProps } from './Exercise';
import { Answer } from '../types';
import styled from 'styled-components';

const exerciseWithStepDataProps: ExerciseWithStepDataProps = {
exercise: {
Expand Down Expand Up @@ -131,6 +132,38 @@ const exerciseWithQuestionStatesProps = (): ExerciseWithQuestionStatesProps => {
},
}};

type TextResizerValue = -2 | -1 | 0 | 1 | 2 | 3;
const textResizerScales = [0.75, 0.9, 1, 1.25, 1.5, 2];
const textResizerValues: TextResizerValue[] = [-2, -1, 0, 1, 2, 3];
const textResizerValueMap = new Map(textResizerValues.map((v, i) => [v, textResizerScales[i]]));

const ExerciseWrapper = styled.div<{ textSize: TextResizerValue }>`
${(props: { textSize: TextResizerValue }) => `
--content-text-scale: ${textResizerValueMap.get(props.textSize)};
`}
`;

const TextResizerProvider = ({ children }: { children: React.ReactNode }) => {
const [index, setIndex] = useState(2);

const increase = () => setIndex(Math.min(index + 1, textResizerValues.length - 1));
const decrease = () => setIndex(Math.max(index - 1, 0));

return (
<ExerciseWrapper textSize={textResizerValues[index]}>
<div style={{ marginBottom: '2rem', alignItems: 'center', placeContent: 'center', display: 'flex', gap: '1rem' }}>
<h3>Text Size</h3>
<button onClick={decrease}>- Decrease</button>
<span style={{ display: 'inline-block', width: '3rem', textAlign: 'center' }}>
<b>{textResizerScales[index]}</b>
</span>
<button onClick={increase}>+ Increase</button>
</div>
{children}
</ExerciseWrapper>
);
};

export const Default = () => {
const [selectedAnswerId, setSelectedAnswerId] = useState<number>(0);
const [apiIsPending, setApiIsPending] = useState(false)
Expand All @@ -144,7 +177,8 @@ export const Default = () => {
setSelectedAnswerId(a.id)
}}
onAnswerSave={() => setApiIsPending(true)}
/>)
/>
)
};

export const DeprecatedStepData = () => <Exercise {...exerciseWithStepDataProps} />;
Expand Down Expand Up @@ -173,7 +207,7 @@ export const CompleteWithFeedback = () => {
}
};

return <Exercise {...props} />;
return <TextResizerProvider><Exercise {...props} /></TextResizerProvider>;
};

export const IncorrectWithFeedbackAndSolution = () => {
Expand All @@ -197,7 +231,7 @@ export const IncorrectWithFeedbackAndSolution = () => {
apiIsPending: false
}
};
return <Exercise {...props} />;
return <TextResizerProvider><Exercise {...props} /></TextResizerProvider>;
};

export const IncorrectWithFeedbackAndSolutionWrappingText = () => {
Expand All @@ -223,7 +257,7 @@ export const IncorrectWithFeedbackAndSolutionWrappingText = () => {
};
props.exercise.questions[0].answers[0].content_html = 'A very long correct answer to observe line wrapping at mobile sizes';
props.exercise.questions[0].answers[1].content_html = 'A very long incorrect answer to observe line wrapping at mobile sizes';
return <Exercise {...props} />;
return <TextResizerProvider><Exercise {...props} /></TextResizerProvider>;
};

export const MultiPartHalfComplete = () => {
Expand Down Expand Up @@ -325,7 +359,7 @@ export const MultiPartHalfComplete = () => {
}
};

return <Exercise {...props} />;
return <TextResizerProvider><Exercise {...props} /></TextResizerProvider>;
};

export const Icons = () => {
Expand All @@ -341,20 +375,20 @@ export const Icons = () => {
};

return <Exercise
{...exerciseWithQuestionStatesProps()}
exerciseIcons={{
topic: {
url: 'https://openstax.org',
location
},
errata: {
url: 'https://openstax.org',
location
},
info: {
location
},
}}
{...exerciseWithQuestionStatesProps()}
exerciseIcons={{
topic: {
url: 'https://openstax.org',
location
},
errata: {
url: 'https://openstax.org',
location
},
info: {
location
},
}}
/>;
};

Expand Down Expand Up @@ -506,7 +540,7 @@ bitterness. The discriminant <span data-math="b^2 - 4ac"></span> could perhaps a
};

return (
<>
<TextResizerProvider>
<Exercise {...props1}
onAnswerChange={(a: Omit<Answer, 'id'> & { id: number, question_id: number }) => {
setSelectedAnswerId(a.id)
Expand All @@ -516,6 +550,6 @@ bitterness. The discriminant <span data-math="b^2 - 4ac"></span> could perhaps a
}}
/>
<Exercise {...props2} />
</>
</TextResizerProvider>
);
};
21 changes: 15 additions & 6 deletions src/components/Exercise.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import scrollToElement from 'scroll-to-element';
import styled, { css } from 'styled-components';
import styled, { createGlobalStyle, css } from 'styled-components';
import { Answer, ExerciseData, ID, QuestionState, StepBase, StepWithData } from '../../src/types';
import { InnerStepCard, OuterStepCard, TaskStepCard, TaskStepCardProps } from './Card';
import { Content } from './Content';
Expand All @@ -12,8 +12,14 @@ import { ExerciseHeaderIcons } from './ExerciseHeaderIcons';
import { TypesetMathContext } from '../hooks/useTypesetMath';

const StyledTaskStepCard = styled(TaskStepCard)`
font-size: 1.8rem;
line-height: 2.8rem;
font-size: calc(1.8rem * var(--content-text-scale));
line-height: calc(2.8rem * var(--content-text-scale));
`;

const GlobalStyle = createGlobalStyle`
:root {
--content-text-scale: 1;
}
`;

const ToolbarWrapper = styled.div<{
Expand Down Expand Up @@ -151,9 +157,9 @@ export interface ExerciseWithQuestionStatesProps extends ExerciseBaseProps {
onAnswerChange: (answer: Omit<Answer, 'id'> & { id: number, question_id: number }) => void;
}

export const Exercise = ({
export const Exercise = styled(({
numberOfQuestions, questionNumber, step, exercise, show_all_feedback, scrollToQuestion, exerciseIcons, ...props
}: ExerciseWithStepDataProps | ExerciseWithQuestionStatesProps) => {
}: { className?: string } & (ExerciseWithStepDataProps | ExerciseWithQuestionStatesProps)) => {
const legacyStepRender = 'feedback_html' in step;
const questionsRef = React.useRef<Array<HTMLDivElement>>([]);
const container = React.useRef<HTMLDivElement>(null);
Expand All @@ -175,6 +181,7 @@ export const Exercise = ({
const mobileToolbarEnabled = Object.values(exerciseIcons || {}).some(({ location }) => location?.toolbar?.mobile);

return <TypesetMathContext.Provider value={typesetExercise}>
<GlobalStyle />
<TaskStepCardWithToolbar
step={step}
questionNumber={questionNumber}
Expand All @@ -184,6 +191,7 @@ export const Exercise = ({
desktopToolbarEnabled={desktopToolbarEnabled}
mobileToolbarEnabled={mobileToolbarEnabled}
{...(exerciseIcons ? { exerciseIcons: exerciseIcons } : null)}
className={props.className}
>
<div ref={container}>
<Preamble exercise={exercise} />
Expand Down Expand Up @@ -217,4 +225,5 @@ export const Exercise = ({
</div>
</TaskStepCardWithToolbar>
</TypesetMathContext.Provider>;
};
})`
`;
6 changes: 3 additions & 3 deletions src/components/Question.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const StyledQuestion = styled.div`
&.openstax-question {
border-top: 1px solid ${colors.palette.pale};
font-size: 1.8rem;
font-size: calc(1.8rem * var(--content-text-scale));
.detailed-solution {
margin-bottom: 1rem;
Expand Down Expand Up @@ -42,8 +42,8 @@ const StyledQuestion = styled.div`
.answers-table {
margin-bottom: 20px;
font-size: 1.6rem;
line-height: 2rem;
font-size: calc(1.6rem * var(--content-text-scale));
line-height: calc(2rem * var(--content-text-scale));
}
.instructions {
Expand Down
5 changes: 3 additions & 2 deletions src/components/StepCardFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ export const StepCardFooter = styled.div`
display: flex;
flex-wrap: wrap;
justify-content: space-between;
font-size: 1.4rem;
line-height: 2rem;
font-size: calc(1.4rem * var(--content-text-scale));
line-height: calc(2rem * var(--content-text-scale));
background: ${colors.card.body.background};
overflow: auto;
> * {
flex-grow: 1;
Expand Down
6 changes: 3 additions & 3 deletions src/components/__snapshots__/Answer.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ exports[`Answer renders a correct answer 1`] = `
className="answer-answer"
>
<div
className="sc-bczRLJ bwhOfi"
className="sc-bczRLJ jEysTE"
>
Correct
Answer
Expand Down Expand Up @@ -194,7 +194,7 @@ exports[`Answer renders an incorrect answer 1`] = `
className="answer-answer"
>
<div
className="sc-bczRLJ gojKWV"
className="sc-bczRLJ dfvfuH"
>
Incorrect
Answer
Expand Down Expand Up @@ -320,7 +320,7 @@ exports[`Answer renders teacher preview 1`] = `
className="answer-answer"
>
<div
className="sc-bczRLJ bwhOfi"
className="sc-bczRLJ jEysTE"
>
Correct
Answer
Expand Down
Loading

0 comments on commit 37e3f8c

Please sign in to comment.