Using @property in styled components: a guide
P粉808697471
P粉808697471 2024-03-28 13:35:39
0
1
383

I want to use animation in my application but it requires @property function in SCSS:

@property --border-angle {
  syntax: "<angle>";
  inherits: true;
  initial-value: 0turn;
}

Is there a way to do this in a styled component?

The complete code of the animation is located at: https://codepen.io/shshaw/pen/RwJwJJx

Or how to rewrite this function so that it does not have to use the property function?

P粉808697471
P粉808697471

reply all(1)
P粉132730839

As I tested, the code posted does seem to work with styled-components, although browser support for @property still seems to be limited , for example it works on Chrome, but currently not on Firefox, so the gradient animation won't play on it.

I tried creating an alternative version of the posted code without using @property and it also works on Firefox. If it helps, here's a demo: stackblitz (code included at the end of the answer).

The code originally posted was tested using the following example: stackblitz (Firefox does not currently support gradient animations for @property).

// Styled components
const Container = styled.div`
  height: 100%;
  background: #223;
  display: grid;
  place-items: center;
`;

const Box = styled.div`
  --border-size: 3px;
  --border-angle: 0turn;
  width: 60vmin;
  height: 50vmin;
  background-image: conic-gradient(
      from var(--border-angle),
      #213,
      #112 50%,
      #213
    ),
    conic-gradient(from var(--border-angle), transparent 20%, #08f, #f03);
  background-size: calc(100% - (var(--border-size) * 2))
      calc(100% - (var(--border-size) * 2)),
    cover;
  background-position: center center;
  background-repeat: no-repeat;
  animation: bg-spin 3s linear infinite;
  @keyframes bg-spin {
    to {
      --border-angle: 1turn;
    }
  }
  &:hover {
    animation-play-state: paused;
  }
  @property --border-angle {
    syntax: "";
    inherits: true;
    initial-value: 0turn;
  }
`;

export default function App() {
  return (
    
      
    
  );
}

Below is an alternative version without @property for comparison, which uses pseudo-elements and adds child div to recreate the animation in styled-components .

Live Demo: stackblitz (should also work for Firefox).

// Styled components
const Container = styled.div`
  min-height: 100vh;
  background: #223;
  display: grid;
  place-items: center;
`;

const Box = styled.div`
  width: 60vmin;
  height: 50vmin;
  position: relative;
  overflow: hidden;
  &::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image: conic-gradient(from 0turn, transparent 20%, #08f, #f03);
    animation: fallback-spin 3s linear infinite;
  }
  @keyframes fallback-spin {
    to {
      transform: scale(1000%) rotate(1turn);
    }
  }
  &:hover::before {
    animation-play-state: paused;
  }
  &:hover > div::before {
    animation-play-state: paused;
  }
`;

const Fallback = styled.div`
  position: absolute;
  inset: 3px;
  overflow: hidden;
  background-color: pink;
  &::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image: conic-gradient(from 0turn, #213, #112 50%, #213);
    animation: fallback-spin 3s linear infinite;
  }
  @keyframes fallback-spin {
    to {
      transform: scale(1000%) rotate(1turn);
    }
  }
`;

export default function App() {
  return (
    
      
        
      
    
  );
}
By the way,

@property is the newer standard CSS. For more background information on @property, see MDN. p>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!