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

fix(triangle): Triangles can inherit their color #435

Closed
Closed
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
42 changes: 28 additions & 14 deletions src/mixins/test/__snapshots__/triangle.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

exports[`triangle should default to a transparent background when not passed a backgroundColor 1`] = `
Object {
"borderColor": "transparent transparent transparent red",
"borderColor": "transparent",
"borderLeftColor": "red",
"borderStyle": "solid",
"borderWidth": "5px 0 5px 20px",
"height": "0",
Expand All @@ -12,7 +13,8 @@ Object {

exports[`triangle should generate a proper triangle when passed all parameters 1`] = `
Object {
"borderColor": "black black black red",
"borderColor": "black",
"borderLeftColor": "red",
"borderStyle": "solid",
"borderWidth": "5 0 5 20",
"height": "0",
Expand All @@ -22,7 +24,8 @@ Object {

exports[`triangle should generate a proper triangle when passed all parameters with units on width/height 1`] = `
Object {
"borderColor": "black black black red",
"borderColor": "black",
"borderLeftColor": "red",
"borderStyle": "solid",
"borderWidth": "5em 0 5em 20em",
"height": "0",
Expand All @@ -32,7 +35,8 @@ Object {

exports[`triangle should generate a proper triangle when passed all parameters with units on width/height with float values 1`] = `
Object {
"borderColor": "black black black red",
"borderColor": "black",
"borderLeftColor": "red",
"borderStyle": "solid",
"borderWidth": "5.25em 0 5.25em 20.5em",
"height": "0",
Expand All @@ -42,7 +46,8 @@ Object {

exports[`triangle should generate a proper triangle when passed string values for height and width 1`] = `
Object {
"borderColor": "black black black red",
"borderColor": "black",
"borderLeftColor": "red",
"borderStyle": "solid",
"borderWidth": "5px 0 5px 20px",
"height": "0",
Expand All @@ -53,7 +58,8 @@ Object {
exports[`triangle should properly add rules when block has existing rules 1`] = `
Object {
"background": "red",
"borderColor": "transparent transparent transparent grey",
"borderColor": "transparent",
"borderLeftColor": "grey",
"borderStyle": "solid",
"borderWidth": "5px 0 5px 20px",
"height": "0",
Expand All @@ -63,8 +69,9 @@ Object {

exports[`triangle should properly render bottom pointing arrow with red foregroundColor, width of 20px and height 20px 1`] = `
Object {
"borderColor": "red transparent transparent transparent",
"borderColor": "transparent",
"borderStyle": "solid",
"borderTopColor": "red",
"borderWidth": "20px 5px 0 5px",
"height": "0",
"width": "0",
Expand All @@ -73,7 +80,8 @@ Object {

exports[`triangle should properly render bottomLeft pointing arrow with blue foregroundColor, width of 20px and height 20px 1`] = `
Object {
"borderColor": "transparent transparent transparent blue",
"borderColor": "transparent",
"borderLeftColor": "blue",
"borderStyle": "solid",
"borderWidth": "20px 0 0 20px",
"height": "0",
Expand All @@ -83,7 +91,8 @@ Object {

exports[`triangle should properly render bottomRight pointing arrow with blue foregroundColor, width of 20px and height 20px 1`] = `
Object {
"borderColor": "transparent transparent blue transparent",
"borderBottomColor": "blue",
"borderColor": "transparent",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, key order matters a lot for this particular use case. borderColor will override borderBottomColor and make the element transparent. We had to do some magic in the original implementation to account for this: https://github.com/styled-components/polished/blob/8607d641e27d945612e81a3078b1674200d6286e/src/mixins/triangle.js

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the object always sorted by key or is it just an artifact of the testing framework?

Did you mean using the border color helper or adding !important to borderBottomColor?

"borderStyle": "solid",
"borderWidth": "0 0 20px 20px",
"height": "0",
Expand All @@ -93,7 +102,8 @@ Object {

exports[`triangle should properly render left pointing arrow with blue foregroundColor, width of 10px and height 20px 1`] = `
Object {
"borderColor": "transparent blue transparent transparent",
"borderColor": "transparent",
"borderRightColor": "blue",
"borderStyle": "solid",
"borderWidth": "10px 10px 10px 0",
"height": "0",
Expand All @@ -103,7 +113,8 @@ Object {

exports[`triangle should properly render right pointing arrow with width of 20px and height 10px 1`] = `
Object {
"borderColor": "transparent transparent transparent red",
"borderColor": "transparent",
"borderLeftColor": "red",
"borderStyle": "solid",
"borderWidth": "5px 0 5px 20px",
"height": "0",
Expand All @@ -113,7 +124,8 @@ Object {

exports[`triangle should properly render top pointing arrow with green foregroundColor, width of 20px and height 20px 1`] = `
Object {
"borderColor": "transparent transparent green transparent",
"borderBottomColor": "green",
"borderColor": "transparent",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as above here.

"borderStyle": "solid",
"borderWidth": "0 10px 20px 10px",
"height": "0",
Expand All @@ -123,8 +135,9 @@ Object {

exports[`triangle should properly render topLeft pointing arrow with blue foregroundColor, width of 20px and height 20px 1`] = `
Object {
"borderColor": "blue transparent transparent transparent",
"borderColor": "transparent",
"borderStyle": "solid",
"borderTopColor": "blue",
"borderWidth": "20px 20px 0 0",
"height": "0",
"width": "0",
Expand All @@ -133,7 +146,8 @@ Object {

exports[`triangle should properly render topRight pointing arrow with blue foregroundColor, width of 20px and height 20px 1`] = `
Object {
"borderColor": "transparent blue transparent transparent",
"borderColor": "transparent",
"borderRightColor": "blue",
"borderStyle": "solid",
"borderWidth": "0 20px 20px 0",
"height": "0",
Expand Down
16 changes: 6 additions & 10 deletions src/mixins/triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,20 @@ const getBorderWidth = (
const getBorderColor = (
pointingDirection: SideKeyword,
foregroundColor: string,
backgroundColor: string,
): string => {
switch (pointingDirection) {
case 'top':
case 'bottomRight':
return `${backgroundColor} ${backgroundColor} ${foregroundColor} ${backgroundColor}`
return { borderBottomColor: foregroundColor }
case 'right':
case 'bottomLeft':
return `${backgroundColor} ${backgroundColor} ${backgroundColor} ${foregroundColor}`
return { borderLeftColor: foregroundColor }
case 'bottom':
case 'topLeft':
return `${foregroundColor} ${backgroundColor} ${backgroundColor} ${backgroundColor}`
return { borderTopColor: foregroundColor }
case 'left':
case 'topRight':
return `${backgroundColor} ${foregroundColor} ${backgroundColor} ${backgroundColor}`
return { borderRightColor: foregroundColor }

default:
throw new PolishedError(59)
Expand Down Expand Up @@ -104,11 +103,8 @@ export default function triangle({
return {
width: '0',
height: '0',
borderColor: getBorderColor(
pointingDirection,
foregroundColor,
backgroundColor,
),
borderColor: backgroundColor,
...getBorderColor(pointingDirection, foregroundColor),
borderStyle: 'solid',
borderWidth: getBorderWidth(pointingDirection, heightAndUnit, widthAndUnit),
}
Expand Down