Git Product home page Git Product logo

desmos-react's Introduction

desmos-react

This package provides a wrapper to use the Desmos API in React.

This package is not affiliated with Desmos. To use this in your product or obtain an API key, please reach out to [email protected].

Installation

npm install desmos-react

Usage

import {Expression, GraphingCalculator, useHelperExpression} from "desmos-react";

function Demo() {
  return (
    <GraphingCalculator
      attributes={{className: "calculator"}}
      fontSize={18} keypad projectorMode
    >
      <Expression id="slider" latex="a=3" />
      <Point />
    </GraphingCalculator>
  );
}

/* useHelperExpression() can only be used inside <GraphingCalculator/>,
which is why this couldn't go in <Demo/> */
function Point() {
  const a = useHelperExpression({latex: "a"});

  let label;
  if (a > 0)
    label = "positive x-axis"
  else if (a < 0)
    label = "negative x-axis"
  else
    label = "origin";

  return (
    <Expression id="point" latex="(a,0)" label={label} showLabel />
  );
}

Reference

This package exports four components and two functions. See https://www.desmos.com/api/v1.6/docs/ for the full list of options.

<GraphingCalculator>, <FourFunctionCalculator>, <ScientificCalculator>

These load the various types of calculator Desmos provides. In addition to the Desmos options, these support an attributes prop to attach additional attributes to the <div> hosting the calculator.

Using ref on a calculator will return a ref to the calculator object, not the div. If you need access to the div, use the elt() function below.

<Expression>

Desmos expression. Put these inside <GraphingCalculator>.

useHelperExpression()

Hook for using helper expressions, see above for usage.

elt(calculator)

This returns the <div> hosting a calculator.

desmos-react's People

Contributors

ysulyma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

desmos-react's Issues

ReferenceError: Desmos is not defined

After installing desmos-react with npm, I am presented with this error when including the Usage code in my App.js react app.
Module../node_modules/desmos-react/index.js
node_modules/desmos-react/index.js:135
132 | return forwardRef(component);
133 | }
134 |

135 | export var FourFunctionCalculator = makeBasicCalculator(Desmos.FourFunctionCalculator, "FourFunctionCalculator");
136 | export var ScientificCalculator = makeBasicCalculator(Desmos.ScientificCalculator, "ScientificCalculator");
137 | export function useHelperExpression(opts) {
138 | var calculator = useContext(DesmosContext);

Breaks using Strict Mode in React 18

In React 18, this library does not work with Strict Mode in development mode.

React 18 introduces a change to Strict Mode which mounts and unmounts components.
See quoted section from React Docs:

With Strict Mode starting in React 18, whenever a component mounts in development, React will simulate immediately unmounting and remounting the component.

For some reason, this is breaking the library: Desmos expressions don't load properly. For example, if you put static <Expression> in <ScientificCalculator> (say "x=2"), the expression will load and quickly remove itself (probably because of the mount/unmount). However, dynamic <Expression>s containing some React state (that updates) work fine. The issue is solved when I remove Strict Mode, build for production, or downgrade to React 17. Thus it must be related to this new React 18 change.

I suspect it has to do with refs, and I found this issue. Many report similar symptoms: facebook/react#24670

Example:

<GraphingCalculator>
	<Expression id="function" latex="x^2" />
</GraphingCalculator>

Result (React 18, Strict Mode, development mode):
Result
x^2 appears but vanishes momentarily.

Multiple errors with module

Hi there,

First of all I'm very pleased this module exists - I was looking for something that could do this. Unfortunately, I get multiple errors when I try to run your demo (I've added the script section to my template). My type checker also raises there when I inspect the index.tsx file. A couple of errors I solved by just adding import React from "react" - is it just that the module is out of date, or am I doing something wrong?

All the best,
Mark

ERROR in node_modules/desmos-react/index.tsx
3:55-59
[tsl] ERROR in node_modules/desmos-react/index.tsx(3,56)
      TS2345: Argument of type 'null' is not assignable to parameter of type 'Calculator'.

ERROR in node_modules/desmos-react/index.tsx
40:49-60
[tsl] ERROR in node_modules/desmos-react/index.tsx(40,50)
      TS2345: Argument of type 'HTMLDivElement | undefined' is not assignable to parameter of type 'HTMLElement'.
  Type 'undefined' is not assignable to type 'HTMLElement'.

ERROR in node_modules/desmos-react/index.tsx
58:5-27
[tsl] ERROR in node_modules/desmos-react/index.tsx(58,6)
      TS2686: 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.

ERROR in node_modules/desmos-react/index.tsx
58:28-33
[tsl] ERROR in node_modules/desmos-react/index.tsx(58,29)
      TS2322: Type 'Calculator | undefined' is not assignable to type 'Calculator'.
  Type 'undefined' is not assignable to type 'Calculator'.

ERROR in node_modules/desmos-react/index.tsx
59:7-10
[tsl] ERROR in node_modules/desmos-react/index.tsx(59,8)
      TS2686: 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.

ERROR in node_modules/desmos-react/index.tsx
59:11-14
[tsl] ERROR in node_modules/desmos-react/index.tsx(59,12)
      TS2322: Type 'MutableRefObject<HTMLDivElement | undefined>' is not assignable to type 'LegacyRef<HTMLDivElement> | undefined'.

ERROR in node_modules/desmos-react/index.tsx
75:8-16
[tsl] ERROR in node_modules/desmos-react/index.tsx(75,9)
      TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ExpressionState'.
  No index signature with a parameter of type 'string' was found on type 'ExpressionState'.

ERROR in node_modules/desmos-react/index.tsx
75:21-35
[tsl] ERROR in node_modules/desmos-react/index.tsx(75,22)
      TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ExpressionState'.
  No index signature with a parameter of type 'string' was found on type 'ExpressionState'.

ERROR in node_modules/desmos-react/index.tsx
76:6-15
[tsl] ERROR in node_modules/desmos-react/index.tsx(76,7)
      TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ type?: "expression" | undefined; latex?: string | undefined; color?: string | undefined; lineStyle?: "POINT" | "OPEN" | "CROSS" | "SOLID" | "DASHED" | "DOTTED" | undefined; lineWidth?: string | ... 1 more ... | undefined; ... 18 more ...; labelOrientation?: "ABOVE" | ... 4 more ... | undefined; }'.
  No index signature with a parameter of type 'string' was found on type '{ type?: "expression" | undefined; latex?: string | undefined; color?: string | undefined; lineStyle?: "POINT" | "OPEN" | "CROSS" | "SOLID" | "DASHED" | "DOTTED" | undefined; lineWidth?: string | ... 1 more ... | undefined; ... 18 more ...; labelOrientation?: "ABOVE" | ... 4 more ... | undefined; }'.

ERROR in node_modules/desmos-react/index.tsx
76:18-26
[tsl] ERROR in node_modules/desmos-react/index.tsx(76,19)
      TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ExpressionState'.
  No index signature with a parameter of type 'string' was found on type 'ExpressionState'.

ERROR in node_modules/desmos-react/index.tsx
86:37-39
[tsl] ERROR in node_modules/desmos-react/index.tsx(86,38)
      TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

ERROR in node_modules/desmos-react/index.tsx
121:37-48
[tsl] ERROR in node_modules/desmos-react/index.tsx(121,38)
      TS2345: Argument of type 'HTMLDivElement | undefined' is not assignable to parameter of type 'HTMLElement'.
  Type 'undefined' is not assignable to type 'HTMLElement'.

ERROR in node_modules/desmos-react/index.tsx
141:7-10
[tsl] ERROR in node_modules/desmos-react/index.tsx(141,8)
      TS2686: 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.

ERROR in node_modules/desmos-react/index.tsx
141:11-14
[tsl] ERROR in node_modules/desmos-react/index.tsx(141,12)
      TS2322: Type 'MutableRefObject<HTMLDivElement | undefined>' is not assignable to type 'LegacyRef<HTMLDivElement> | undefined'.
  Type 'MutableRefObject<HTMLDivElement | undefined>' is not assignable to type 'RefObject<HTMLDivElement>'.
    Types of property 'current' are incompatible.
      Type 'HTMLDivElement | undefined' is not assignable to type 'HTMLDivElement | null'.
        Type 'undefined' is not assignable to type 'HTMLDivElement | null'.

ERROR in node_modules/desmos-react/index.tsx
163:4-18
[tsl] ERROR in node_modules/desmos-react/index.tsx(163,5)
      TS2532: Object is possibly 'undefined'.

ERROR in node_modules/desmos-react/index.tsx
163:58-72
[tsl] ERROR in node_modules/desmos-react/index.tsx(163,59)
      TS2532: Object is possibly 'undefined'.

16 errors have detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.

webpack 5.73.0 compiled with 16 errors in 7745 ms

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.