ianmackenzie / elm-units

Simple, safe and convenient unit types and conversions for Elm
https://package.elm-lang.org/packages/ianmackenzie/elm-units/latest/
BSD 3-Clause "New" or "Revised" License
85 stars 14 forks source link

Replace Pixels module with Css.Units? #35

Closed ianmackenzie closed 4 years ago

ianmackenzie commented 5 years ago

I've never liked the Pixels.pixels and Pixels.inPixels function names - one solution would be to have a Css.Units module (intended to be imported as Css), something like:

module Css.Units exposing
    ( Pixels
    , pixels
    , nominalPixelDensity
    )

import Quantity exposing (Quantity)
import Length

type Pixels
    = Pixels

pixels : number -> Quantity number Pixels
pixels numPixels =
    Quantity numPixels

nominalPixelDensity : Quantity Float (Rate Pixels Meters)
nominalPixelDensity =
    pixels 96 |> Quantity.per (Length.inches 1)

Then client code could write

module Main exposing (..)

import Css.Units as Css
import Length
import Quantity

width =
    Css.pixels 200

height =
    Length.inches 3 |> Quantity.at Css.nominalPixelDensity
--> Css.pixels 288

I think Css.pixels is both more explicit (CSS pixels as opposed to physical pixels) and reads better than Pixels.pixels, in addition to being shorter =)

This would ideally be accompanied by a Quantity.in_ function (#27), so you could write

height |> Quantity.in_ Css.pixels
--> 288

instead of needing a Css.inPixels function (what does "CSS in pixels" mean?).

I think this could form a good pattern for similar custom modules in the future, like a Game module exposing a Game.Tiles units type and a Game.tiles function.

Possible extensions:

ianmackenzie commented 4 years ago

I don't think this really makes sense any more given the recent addition of Length.cssPixels - Pixels.pixels is still awkward but it may be possible to fix that by moving everything into one Quantity module (#31).