Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails silently when Compiled APIs are used as values in style attribute #1614

Open
dddlr opened this issue Jan 8, 2024 · 0 comments
Open
Labels
bug 🐛 Something isn't working has workaround 💫 You can work around this bug.

Comments

@dddlr
Copy link
Collaborator

dddlr commented Jan 8, 2024

Keyframes API in style attribute

Take this example:

import { css, keyframes } from '@compiled/react';
import React from 'react';

const slideInTop = keyframes({
    from: {
        transform: 'translateY(15px)',
    },
    to: {
        transform: 'translateY(0)',
    },
});

const slideInBottom = keyframes({
    from: {
        transform: 'translateY(-15px)',
    },
    to: {
        transform: 'translateY(0)',
    },
});

const dragHandleDotStyles = css({
    width: '9px',
    height: '9px',
    borderRadius: '50%',
    cursor: 'pointer',
});

const type = 'top';

export const App = () => (
  <>
    <div
        css={dragHandleDotStyles}
        style={{
            animation: `${type === 'top' ? slideInTop : slideInBottom} 1s`,
        }}
    >
      hello
    </div>
  </>
);

This fails silently because we do not support keyframes API (which uses Compiled) inside of style attribute (which currently bypasses Compiled entirely?).

We also get some bizarre output for the animation property:

Screenshot 2024-01-08 at 17 09 54

We should throw an error here instead.

Workaround for keyframes API in style attribute

Note that there is a workaround - move everything into the css attribute:

const slideInTopStyles = css({
  animation: `${slideInTop} 1s`,
});

const slideInBottomStyles = css({
  animation: `${slideInBottom} 1s`,
})

const type = 'top';

export const App = () => (
  <>
    <div
        data-dot
        css={[dragHandleDotStyles, type === 'top' && slideInTopStyles, type === 'bottom' && slideInBottomStyles]}
    >
      bap
    </div>
  </>
);

We can see that the output CSS is now correct:

Screenshot 2024-01-08 at 17 08 45

Note two: we do not support ternary operators, so we can't do type === 'top' ? slideInTopStyles : slideInBottomStyles in the css attribute just yet. One day!

Use of other Compiled APIs in style attribute

Additionally, using Compiled's css API (and potentially other Compiled APIs, like the cssMap API) also fails silently:

const dragHandleDotStyles = css({
    width: '9px',
    height: '9px',
    borderRadius: '50%',
    cursor: 'pointer',
});

export const App = () => (
  <>
    <div
        style={{
            animation: `${dragHandleDotStyles} 1s`,
        }}
    >
      hello??
    </div>
  </>
);

Regardless of what Compiled API is being used in the style attribute, we should throw an error.

@dddlr dddlr added bug 🐛 Something isn't working has workaround 💫 You can work around this bug. labels Jan 8, 2024
@dddlr dddlr changed the title Fails silently when keyframes API is used in style attribute Fails silently when css or keyframes API is used in style attribute Jan 8, 2024
@dddlr dddlr changed the title Fails silently when css or keyframes API is used in style attribute Fails silently when Compiled APIs are used as values in style attribute Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working has workaround 💫 You can work around this bug.
Projects
None yet
Development

No branches or pull requests

1 participant