Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide keypad for expression story #442

Merged
merged 7 commits into from
Apr 4, 2023
Merged

Conversation

handeyeco
Copy link
Contributor

@handeyeco handeyeco commented Mar 27, 2023

Summary:

I think I'm starting to understand the mobile web keypad:

  • We have a Keypad context (Provider/Consumer)
  • We have the actual Keypad component (math-input/src/components/keypad-container.js)
  • We have the MathInput component

Then we shuffle it all around chaotically. Something like:

function App() {
  const [keypadElement, setKeypadElement] = useState()
  return (
    <KeypadContext.Provider value={{ keypadElement, setKeypadElement }}>
      <KeypadInput keypadElement={keypadElement} />
      <KeypadContext.Consumer>
        {({keypadElement, setKeypadElement}) => (
          <Keypad onElementMounted={setKeypadElement} />
        )}
      </KeypadContext.Consumer>
    </KeypadContext.Provider>
  )
}

Why all the hoop jumping? Still don't know why. Seems like MathInput could just render the input and the keypad without the need for Context, but I'm sure there was a good reason.

ANYWAY

This explains why I wasn't able to see the Keypad on the Expression widget in Storybook: it wasn't using Context. This PR creates a shared component for providing context (with the on-screen-keypad story, which might be obsolete now).

Unfortunately all this indirection kills my dream of having MathInput manage how we render the keypad; I think it's going to have to happen in Perseus unless we really want to overhaul things.

Screen.Recording.2023-03-27.at.4.31.13.PM.mov

@handeyeco handeyeco self-assigned this Mar 27, 2023
@changeset-bot
Copy link

changeset-bot bot commented Mar 27, 2023

🦋 Changeset detected

Latest commit: 511b0d2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Mar 27, 2023

Size Change: 0 B

Total Size: 652 kB

ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38 kB
packages/kmath/dist/es/index.js 4.13 kB
packages/math-input/dist/es/index.js 60.3 kB
packages/perseus-editor/dist/es/index.js 114 kB
packages/perseus-error/dist/es/index.js 705 B
packages/perseus-linter/dist/es/index.js 21.2 kB
packages/perseus/dist/es/index.js 398 kB
packages/pure-markdown/dist/es/index.js 3.65 kB
packages/simple-markdown/dist/es/index.js 12.6 kB

compressed-size-action

@coveralls
Copy link
Collaborator

coveralls commented Mar 27, 2023

Coverage Status

Coverage: 43.018% (+0.0005%) from 43.017% when pulling 511b0d2 on provide-expression-story into ec4389e on main.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 27, 2023

npm Snapshot: Published

🎉 Good news!! We've packaged up the latest commit from this PR (c4805c6) and published it to npm. You
can install it using the tag PR442.

Example:

yarn add @khanacademy/perseus@PR442

<KeypadContext.Consumer>
{({keypadElement, setRenderer, scrollableElement}) => {
return (
<ItemRendererWithDebugUI
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to ItemRendererWithDebugUI because ItemRenderer has keypadElement={this.keypadElement()} when rendering Renderer whereas RendererWithDebugUI does not - which was causing errors when trying to open the keypad.

I imagine I could have added it to RendererWithDebugUI, but I'm not totally sure I understand why we have both ItemRendererWithDebugUI and RendererWithDebugUI. Figured it was best to let sleeping dogs lie.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two "RendererWithDebugUI" helpers render different components. In Perseus, we have ItemRenderer and Renderer. The former is high-level and accepts a bigger blob of JSON, the Renderer is our core, lower-level component that renders a single PerseusRenderer object (the thing that looks like {content: string, widgets: {}, images: {}}.

@@ -4,60 +4,11 @@ import {View} from "@khanacademy/wonder-blocks-core";
import {StyleSheet} from "aphrodite";
import * as React from "react";

import {ItemRenderer} from "../../index.js";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I split this logic out so both on-screen-keypad.stories.jsx and expression.stories.jsx could use the same keypad provider. However (IMO) on-screen-keypad.stories.jsx was really just a very specific expression story, so I ended up just strengthening up expression.stories.jsx and getting rid of on-screen-keypad.stories.jsx.

Keeping this split out logic separate though, because (I think) we want the keypad in lots of components. Also I didn't want to clutter expression.stories.jsx.

const createItemJson = (
widgetOptions: PerseusExpressionWidgetOptions,
version: Version,
): Item => {
): PerseusItem => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the real PerseusItem type instead of the fake Item. That required adding a few fields to the object.

@handeyeco handeyeco marked this pull request as ready for review March 28, 2023 15:47
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Mar 28, 2023

Gerald

Required Reviewers
  • @Khan/perseus for changes to .changeset/thick-islands-kick.md, packages/perseus/src/widgets/__stories__/expression.stories.tsx, packages/perseus/src/widgets/__stories__/test-keypad-context-wrapper.tsx, packages/perseus/src/widgets/__testdata__/expression.testdata.ts

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

@handeyeco handeyeco requested a review from a team March 28, 2023 15:49
Copy link
Collaborator

@jeremywiebe jeremywiebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Glad you feel you're starting to understand all this Math Input stuff... it's still very much a mystery to me! :)

<KeypadContext.Consumer>
{({keypadElement, setRenderer, scrollableElement}) => {
return (
<ItemRendererWithDebugUI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two "RendererWithDebugUI" helpers render different components. In Perseus, we have ItemRenderer and Renderer. The former is high-level and accepts a bigger blob of JSON, the Renderer is our core, lower-level component that renders a single PerseusRenderer object (the thing that looks like {content: string, widgets: {}, images: {}}.

@handeyeco handeyeco merged commit f1ffa47 into main Apr 4, 2023
@handeyeco handeyeco deleted the provide-expression-story branch April 4, 2023 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants