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

Handle undefined style #11

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 90 additions & 99 deletions src/ReactColorA11y.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from 'react';
import { colord } from 'colord';
import ReactColorA11y from './ReactColorA11y';
/// <reference types="Cypress" />
/// <reference types="../cypress/cypress.d.ts" />

import React from 'react'
import { colord } from 'colord'
import ReactColorA11y from './ReactColorA11y'

const expectColorsToMatch = (color1: string, color2: string) => {
expect(colord(color1).toHex()).to.be.equal(colord(color2).toHex());
expect(colord(color1).toHex()).to.be.equal(colord(color2).toHex())
}

describe('ReactColorA11y', () => {
Expand All @@ -14,10 +17,10 @@ describe('ReactColorA11y', () => {
{ original: 'rgb(255, 100, 200)', lighter: 'rgb(255, 100, 200)', darker: 'rgb(197, 0, 127)' },
{ original: 'rgb(120, 255, 120)', lighter: 'rgb(120, 255, 120)', darker: 'rgb(0, 120, 0)' },
{ original: 'rgb(255, 255, 255)', lighter: 'rgb(255, 255, 255)', darker: 'rgb(0, 0, 0)' }
];
]

const lightBackground = 'rgb(230, 230, 230)';
const darkBackground = 'rgb(25, 25, 25)';
const lightBackground = 'rgb(230, 230, 230)'
const darkBackground = 'rgb(25, 25, 25)'

it('should lighten colors until required contrast reached', () => {
cy.mount(
Expand All @@ -28,15 +31,15 @@ describe('ReactColorA11y', () => {
))}
</ReactColorA11y>
</div>
);
)

cy.wait(100);
cy.wait(100)

expectedColorMappings.forEach(({ original, lighter }) => {
cy.contains(`${original} text`).invoke('css', 'color')
.then((color: string) => expectColorsToMatch(color, lighter));
});
});
.then((color: string) => expectColorsToMatch(color, lighter))
})
})

it('should darken colors until required contrast reached', () => {
cy.mount(
Expand All @@ -47,15 +50,15 @@ describe('ReactColorA11y', () => {
))}
</ReactColorA11y>
</div>
);
)

cy.wait(100);
cy.wait(100)

expectedColorMappings.forEach(({ original, darker }) => {
cy.contains(`${original} text`).invoke('css', 'color')
.then((color: string) => expectColorsToMatch(color, darker));
});
});
.then((color: string) => expectColorsToMatch(color, darker))
})
})

it('should handle svg fill and stroke', () => {
cy.mount(
Expand All @@ -68,131 +71,119 @@ describe('ReactColorA11y', () => {
</svg>
</ReactColorA11y>
</div>
);
)

cy.wait(100);
cy.wait(100)

expectedColorMappings.forEach(({ lighter }, index) => {
cy.get(`#${index}`).then(($element: any) => {
expectColorsToMatch($element.attr('fill'), lighter);
expectColorsToMatch($element.attr('stroke'), lighter);
expectColorsToMatch($element.css('fill'), lighter);
expectColorsToMatch($element.css('stroke'), lighter);
});
});
});
expectColorsToMatch($element.attr('fill'), lighter)
expectColorsToMatch($element.attr('stroke'), lighter)
expectColorsToMatch($element.css('fill'), lighter)
expectColorsToMatch($element.css('stroke'), lighter)
})
})
})

describe('preserveContrastDirectionIfPossible', () => {
describe('lighter starting direction', () => {
const backgroundColor = 'rgb(150, 150, 150)';
const foregroundColor = 'rgb(151, 151, 151)';
const backgroundColor = 'rgb(150, 150, 150)'
const foregroundColor = 'rgb(151, 151, 151)'

it('should preserve lighter color if possible', () => {
cy.mount(
<>
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={2}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
</>
);
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={2}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
)

cy.wait(100);
cy.wait(100)

cy.contains('text').invoke('css', 'color')
.then((color: string) => expectColorsToMatch(color, 'rgb(212, 212, 212)'));
});
.then((color: string) => expectColorsToMatch(color, 'rgb(212, 212, 212)'))
})

it('should not preserve lighter color if that behavior is requested', () => {
cy.mount(
<>
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={2} preserveContrastDirectionIfPossible={false}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
</>
);
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={2} preserveContrastDirectionIfPossible={false}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
)

cy.wait(100);
cy.wait(100)

cy.contains('text').invoke('css', 'color')
.then((color: string) => expectColorsToMatch(color, 'rgb(97, 97, 97)'));
});
.then((color: string) => expectColorsToMatch(color, 'rgb(97, 97, 97)'))
})

it('should switch to darker color if needed to meet contrast', () => {
cy.mount(
<>
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={4.5}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
</>
);
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={4.5}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
)

cy.wait(100);
cy.wait(100)

cy.contains('text').invoke('css', 'color')
.then((color: string) => expectColorsToMatch(color, 'rgb(41, 41, 41)'));
});
});
.then((color: string) => expectColorsToMatch(color, 'rgb(41, 41, 41)'))
})
})

describe('darker starting direction', () => {
const backgroundColor = 'rgb(100, 100, 100)';
const foregroundColor = 'rgb(99, 99, 99)';
const backgroundColor = 'rgb(100, 100, 100)'
const foregroundColor = 'rgb(99, 99, 99)'

it('should preserve darker color if possible', () => {
cy.mount(
<>
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={2}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
</>
);
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={2}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
)

cy.wait(100);
cy.wait(100)

cy.contains('text').invoke('css', 'color')
.then((color: string) => expectColorsToMatch(color, 'rgb(51, 51, 51)'));
});
.then((color: string) => expectColorsToMatch(color, 'rgb(51, 51, 51)'))
})

it('should not preserve darker color if that behavior is requested', () => {
cy.mount(
<>
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={2} preserveContrastDirectionIfPossible={false}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
</>
);
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={2} preserveContrastDirectionIfPossible={false}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
)

cy.wait(100);
cy.wait(100)

cy.contains('text').invoke('css', 'color')
.then((color: string) => expectColorsToMatch(color, 'rgb(153, 153, 153)'));
});
.then((color: string) => expectColorsToMatch(color, 'rgb(153, 153, 153)'))
})

it('should switch to lighter color if needed to meet contrast', () => {
cy.mount(
<>
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={4.5}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
</>
);
<div style={{ backgroundColor }}>
<ReactColorA11y requiredContrastRatio={4.5}>
<p style={{ color: foregroundColor }}>{'text'}</p>
</ReactColorA11y>
</div>
)

cy.wait(100);
cy.wait(100)

cy.contains('text').invoke('css', 'color')
.then((color: string) => expectColorsToMatch(color, 'rgb(227, 227, 227)'));
});
});
.then((color: string) => expectColorsToMatch(color, 'rgb(227, 227, 227)'))
})
})
})
});
})
22 changes: 12 additions & 10 deletions src/ReactColorA11y.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,17 @@ const ReactColorA11y: React.FunctionComponent<ReactColorA11yProps> = ({
element.setAttribute('stroke', calculateA11yColor(backgroundColor, strokeColor))
}

const { color: computedColor, stroke: computedStroke, fill: computedFill } = getComputedStyle(element)
if (computedColor !== null) {
element.style.color = calculateA11yColor(backgroundColor, computedColor)
}
if (computedFill !== null) {
element.style.fill = calculateA11yColor(backgroundColor, computedFill)
}
if (computedStroke !== null) {
element.style.stroke = calculateA11yColor(backgroundColor, computedStroke)
if (element.style !== undefined) {
const { color: computedColor, stroke: computedStroke, fill: computedFill } = getComputedStyle(element)
if (computedColor !== null) {
element.style.color = calculateA11yColor(backgroundColor, computedColor)
}
if (computedFill !== null) {
element.style.fill = calculateA11yColor(backgroundColor, computedFill)
}
if (computedStroke !== null) {
element.style.stroke = calculateA11yColor(backgroundColor, computedStroke)
}
}
}

Expand All @@ -174,7 +176,7 @@ const ReactColorA11y: React.FunctionComponent<ReactColorA11yProps> = ({

useEffect(() => {
if (reactColorA11yRef.current === null || reactColorA11yRef.current === undefined) {
return () => {}
return () => { }
}

const mutationCallback = (): void => {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
Expand Down
Loading