Skip to content

Commit

Permalink
Properly prefix keyframes
Browse files Browse the repository at this point in the history
Use `inline-style-prefixer`’s `prefixedKeyframes`
Fixes FormidableLabs#488
  • Loading branch information
ianobermiller committed Jan 7, 2016
1 parent b620aba commit 747587b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
2 changes: 2 additions & 0 deletions interfaces/inline-style-prefixer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
declare class InlineStylePrefixer {
static prefixAll(style: Object): Object;

prefixedKeyframes: string;

constructor(config: {
keepUnprefixed?: bool,
userAgent?: ?string
Expand Down
32 changes: 29 additions & 3 deletions src/__tests__/keyframes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,33 @@ import {expectCSS, getElement} from 'test-helpers';
import React, {Component} from 'react';
import TestUtils from 'react-addons-test-utils';

const CHROME_14_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 ' +
'(KHTML, like Gecko) Chrome/14.0.812.0 Safari/535.1';

describe('keyframes', () => {
it('adds prefix to keyframes when needed', () => {
const anim = keyframes({from: {left: 0}, to: {left: 100}}, 'slide');
class TestComponent extends Component {
render() {
return (
<StyleRoot
radiumConfig={{userAgent: CHROME_14_USER_AGENT}}
style={{animationName: anim}}
/>
);
}
}

const output = TestUtils.renderIntoDocument(<TestComponent />);
const style = getElement(output, 'style');

expectCSS(style, `
@-webkit-keyframes slide-radium-animation-1bdcc98d {
from {left: 0;}
to {left: 100px;}
}
`);
});

it('renders keyframes in root style component', () => {
const animation = keyframes({
Expand All @@ -24,7 +50,7 @@ describe('keyframes', () => {
const style = getElement(output, 'style');

expectCSS(style, `
@-webkit-keyframes SlideFromLeft-radium-animation-1b668a10 {
@keyframes SlideFromLeft-radium-animation-1b668a10 {
from{
left: -1000px;
}
Expand Down Expand Up @@ -52,7 +78,7 @@ describe('keyframes', () => {
const style = getElement(output, 'style');

expectCSS(style, `
@-webkit-keyframes SlideFromLeft-radium-animation-ab5ed129 {
@keyframes SlideFromLeft-radium-animation-ab5ed129 {
from{
left: -1000px;
}
Expand Down Expand Up @@ -92,7 +118,7 @@ describe('keyframes', () => {
const style = getElement(output, 'style');

expectCSS(style, `
@-webkit-keyframes SlideFromLeft-radium-animation-1b668a10 {
@keyframes SlideFromLeft-radium-animation-1b668a10 {
from{
left: -1000px;
}
Expand Down
3 changes: 2 additions & 1 deletion src/keyframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cssRuleSetToString from './css-rule-set-to-string';
import hash from './hash';
import {getPrefixedKeyframes} from './prefixer';

export type Keyframes = {
__radiumKeyframes: bool,
Expand All @@ -15,7 +16,7 @@ export default function keyframes(
return {
__radiumKeyframes: true,
__process(userAgent) {
const keyframesPrefixed = '-webkit-keyframes';
const keyframesPrefixed = getPrefixedKeyframes(userAgent);
const rules = Object.keys(keyframeRules).map(percentage =>
cssRuleSetToString(
percentage,
Expand Down
20 changes: 14 additions & 6 deletions src/prefixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ let hasWarnedAboutUserAgent = false;
let lastUserAgent;
let prefixer;

// Returns a new style object with vendor prefixes added to property names
// and values.
export function getPrefixedStyle(
style: Object,
userAgent?: ?string,
): Object {
function getPrefixer(userAgent: ?string): InlineStylePrefixer {
const actualUserAgent = userAgent ||
(global && global.navigator && global.navigator.userAgent);

Expand All @@ -48,7 +43,20 @@ export function getPrefixedStyle(
prefixer = new InlineStylePrefixer({userAgent: actualUserAgent});
lastUserAgent = actualUserAgent;
}
return prefixer;
}

export function getPrefixedKeyframes(userAgent?: ?string): string {
return getPrefixer(userAgent).prefixedKeyframes;
}

// Returns a new style object with vendor prefixes added to property names
// and values.
export function getPrefixedStyle(
style: Object,
userAgent?: ?string,
): Object {
const prefixer = getPrefixer(userAgent);
const prefixedStyle = prefixer.prefix(style);
const prefixedStyleWithFallbacks = transformValues(prefixedStyle);
return prefixedStyleWithFallbacks;
Expand Down

0 comments on commit 747587b

Please sign in to comment.