question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add TypeScript types

See original GitHub issue

This library is great, but it was a struggle to get going with TS. Here is what I have so far:

declare module 'svg-gauge' {
  export interface Instance {
    setMaxValue: (max: number) => void
    setValue: (val: number) => void
    setValueAnimated: (val: number, duration: number) => void
    getValue: () => number
  }

  export interface Opts {
    viewBox: string
    /**
     * The angle in degrees to start the dial
     * @default 135
     */
    dialStartAngle?: number
    /** The angle in degrees to end the dial. This MUST be less than dialStartAngle
     * @default 45
     */
    dialEndAngle?: number
    /** The radius of the gauge
     * @default 40
     */
    radius?: number
    /** The minimum value for the gauge. This can be a negative value
     * @default 0
     */
    min?: number
    /** The maximum value for the gauge
     * @default 100
     */
    max?: number
    /** Getter for the label that will be rendered in the center. */
    label?: (currentValue: number) => string
    /** Whether to show the value at the center of the gauge
     * @default true
     */
    showValue?: boolean
    /** 	The CSS class of the gauge
     * @default 'gauge'
     */
    gaugeClass?: string
    /** The CSS class of the gauge's dial
     * @default 'dial'
     */
    dialClass?: string
    /** The CSS class of the gauge's fill
     * @default 'value'
     */
    valueDialClass?: string
    /** The CSS class of the gauge's text
     * @default '(value-text)'
     */
    valueClass?: string
    /**
     * An optional function that can return a color for current value
     */
    color?: (currentValue: number) => string
    /** An optional string that specifies the crop region
     * @default '(0 0 100 100)'
     */
    viewBox?: string
  }

  export = (element: HTMLDivElement, props: Partial<Opts>) => Instance
}

Then for usage, the provided React example converted to use these types

import React, { FC, useEffect, useRef } from 'react'
import SvgGauge, { Opts, Instance } from 'svg-gauge'

const defaultOptions: Partial<Opts> = {
  max: 100
}

const Gauge: FC<{ value: number }> = ({ value }) => {
  const gaugeEl = useRef<HTMLDivElement>(null)
  const gaugeRef = useRef<Instance | null>(null)
  useEffect(() => {
    if (!gaugeRef.current) {
      if (!gaugeEl.current) return
      const options = { ...defaultOptions, color: (value) => value < 70 ? 'green' : 'yellow' }
      gaugeRef.current = SvgGauge(gaugeEl.current, options)
      gaugeRef.current?.setValue(1)
    }
    gaugeRef.current?.setValueAnimated(value, 1)
  }, [value])

  return (
    <div style={{ height: '500px', width: '500px' }}>
      <div ref={gaugeEl} className='gauge-container' style={{ position: 'relative' }}>
        <span style={{ position: 'absolute', left: '45%', bottom: '70%' }}>km/hr</span>
      </div>
    </div>
  )
}

I’m rather unfamiliar with the library, so i could be way off. This is just what I have so far and is working for my simplistic use case.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
christopher-caldwellcommented, Nov 9, 2021

Bump

0reactions
naikuscommented, Nov 13, 2021

Done

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I add TypeScript types to a JavaScript module without ...
I have an npm module that is written in JavaScript, not TypeScript. Some of the users of this module are using TypeScript, so...
Read more >
Documentation - Advanced Types - TypeScript
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >
TypeScript: Adding Custom Type Definitions for Existing ...
In these circumstances, you have to add your own custom type definitions for the libraries. This article will show you how to that....
Read more >
How To Create Custom Types in TypeScript - DigitalOcean
Here you create a normal type with the type definition block in curly brackets ( {} ), and then add a special property...
Read more >
@types/typescript | Yarn - Package Manager
This is a stub types definition for TypeScript (https://github.com/Microsoft/TypeScript). TypeScript provides its own type definitions, so you don't need ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found