Skip to content

Add support for simple duration values #342

@CITguy

Description

@CITguy

Before starting

  • I have searched existing issues and there is not already an open issue on the subject.

Summary

I propose extending the duration token type to also accept a JSON number value in addition to the current object value.

  • duration: Number | Object
    • Object
      • value: number
      • unit: string (ms or s)
    • Number (duration in ms)
      • <value> equivalent to { value: <value>, unit: 'ms' }

Version

Third Editor’s Draft

Problem & motivation

I get why the granularity of "value" vs "unit" was done for dimension tokens, given the variety of dimensional units and complex conversion formulas between them.

However, a duration (i.e., length of time) has a single set of globally-known ratios to be able to convert a base unit (ms) to a larger order unit (s, min, hour, etc.). The vast majority of humans that can tell time know of these ratios (1000ms = 1s, 60s = 1min, etc.).

There might be accuracy concerns when converting to larger and larger unit values, but I'd argue that design tokens (which are used to build UI interactions that appear for seconds; at most), should rarely ever need units large enough for accuracy to be an issue.

Prior art

No response

Pros & cons

Pros

  • backward-compatible syntax
  • less-verbose syntax
    • easy to define manually for the majority of practical durations (< 5min)
    • for much larger values, software-defined values can take advantage of scientific notation to optimize the JSON definition
    • some authors may prefer this syntax

Cons

  • additional data type for tooling authors to support
  • implementation of larger unit conversion (e.g., to s, min, etc.) falls on tooling authors
  • very large durations are difficult to define manually without the aid of a calculator
    • (may not be a problem for most practical use cases)

Examples

250ms

(all tokens are equivalent)

{
  "tok1": {
    // most explicit / user-friendly
    "a": {
      "$type": "duration",
      "$value": {
        "value": 250,
        "unit": "ms"
      }
    },
    "b": {
      "$type": "duration",
      "$value": {
        "value": 0.25,
        "unit": "s"
      }
    },
    // most efficient
    "c": {
      "$type": "duration",
      "$value": 250
    }
  }
}

1.5s

(all tokens are equivalent)

{
  "tok2": {
    // most explicit / user-friendly
    "a": {
      "$type": "duration",
      "$value": {
        "value": 1.5,
        "unit": "s"
      }
    },
    "b": {
      "$type": "duration",
      "$value": {
        "value": 1500,
        "unit": "ms"
      }
    },
    // most efficient
    "c": {
      "$type": "duration",
      "$value": 1500
    }
  }
}

3 3/8min

This example may not be practical, but it's possible.

(all tokens are equivalent)

{
  "tok3": {
    // most explicit / user-friendly
    "a": {
      "$type": "duration",
      "$value": {
        "value": 202.5,
        "unit": "s"
      }
    },
    "b": {
      "$type": "duration",
      "$value": {
        "value": 202500,
        "unit": "ms"
      }
    },
    "c": {
      "$type": "duration",
      "$value": {
        "value": 2.025e5,
        "unit": "ms"
      }
    },
    "d": {
      "$type": "duration",
      "$value": 2.025e5
    },
    // most efficient
    "e": {
      "$type": "duration",
      "$value": 202500
    }
  }
}

Alternatives

No response

Required

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions