Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load keyframe from imported (@use) Sass file?

Tags:

css

sass

(Dart Sass) I am attempting to load the following keyframes located in _global.sass into local.sass. I have tried several implementations of calling bgGradient, but none have worked so far.

_global.sass

@keyframes bgGradient
    0%
        background-position: 0% 50%
    50%
        background-position: 100% 50%
    100%
        background-position: 0% 50%

local.sass

@use '_global' as global
div.wrapper
    animation: global.bgGradient 15s ease 

Receiving the following error:

Syntax Error: SassError: expected "(".
    animation: global.bgGradient 15s ease infinite
                                ^
like image 311
Ryan Prentiss Avatar asked Oct 17 '25 23:10

Ryan Prentiss


1 Answers

@keyframes is not a scss/sass feature but rather plain css. Therefore you cannot "use" it like you would variables, mixins or functions.

Input:

// _globals.sass

$width: 20px

@keyframes bgGradient
    0%
        background-position: 0% 50%
    50%
        background-position: 100% 50%
    100%
        background-position: 0% 50%

.foo
    width: $width
// local.sass

@use 'global'

div.wrapper
    width: global.$width
    animation: bgGradient 15s ease 

Output:

@keyframes bgGradient {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.foo {
    width: 20px;
}

div.wrapper {
    width: 20px;
    animation: bgGradient 15s ease;
}

Note how .foo and @keyframes bgGradient are both included in the output. This means that as long as you @use the partial that has some keyframes somewhere you'll be able to use those keyframes throughout all the sass files that make up your local.css file, Just like @import would work.

like image 107
vjee Avatar answered Oct 19 '25 13:10

vjee