Skip to content

Using calc in a transform expression #495

Open
@MelvIsntNormal

Description

@MelvIsntNormal

I can't seem to use a calc expression for a transformation. I'm trying to write:

.tooltip {
    /*--- snip ---*/
    transform: translateX(calc(0px - var(--tooltip-offset)));
}

But (what I think is) the elm-css equivalent does not compile:

tooltip : LengthOrAuto (Calc compatible) -> Style
tooltip offset =
    batch
        [ {--- snip ---}
        , transform (translateX (calc (px 0) minus offset))
        ]

I get this error:

-- TYPE MISMATCH ------------------------------------ ./src/App/View/Tooltip.elm

The 1st argument to `translateX` is not what I expect:

115|                         [ transform (translateX (calc (px 0) minus offset))
                                                      ^^^^^^^^^^^^^^^^^^^^^^^^
This `calc` call produces:

    CalculatedLength

But `translateX` needs the 1st argument to be:

    { compatible
        | length : Css.Structure.Compatible
        , numericValue : Float
        , unitLabel : String
        , units : units
        , value : String
    }

Hint: Seems like a record field typo. Maybe numericValue should be value?

Hint: Can more type annotations be added? Type annotations always help me give
more specific messages, and I think they could help a lot in this case!

Activity

tgelu

tgelu commented on Oct 21, 2020

@tgelu

a bit late, but did you manage to figure this out? I am struggling with a similar case

sebbes-at-deepgram

sebbes-at-deepgram commented on May 29, 2021

@sebbes-at-deepgram

Same here!

sebbes-at-deepgram

sebbes-at-deepgram commented on May 29, 2021

@sebbes-at-deepgram

Workaround: in regular old css, rather than calc'ing the translation, you can actually use a series of translates, as long as they're in the same property. So we can do:

Css.transforms
      [ Css.translateX <| Css.px <| 48
      , Css.translateX <| Css.pct <| 50
      ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @tgelu@MelvIsntNormal@sebbes-at-deepgram

        Issue actions

          Using calc in a transform expression · Issue #495 · rtfeldman/elm-css