Skip to content

Commit

Permalink
Improve background
Browse files Browse the repository at this point in the history
  • Loading branch information
miqayelsrapionyan committed Aug 13, 2024
1 parent 15569fc commit 3e7878b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/Strategies/BackgroundStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CssGenerator\Strategies;

use function join;
use function is_string;

class BackgroundStrategy implements StrategyInterfaceWithMediaMapping
{
Expand Down Expand Up @@ -35,6 +36,10 @@ public function convert(array $variantStyle, array $mediaMapping = []): string
continue;
}

if ($variantStyleValue['type'] === 'solid' && (is_string($variantStyleValue['value']) || empty($variantStyleValue['value'][1]))) {
return 'background: '.$variantStyleValue['value'].';';
}

$color = $this->parseValue($variantStyleValue['type'], $variantStyleValue['value'], $mediaMapping);

$data = $variantStyleValue['type'] === 'image' ? $variantStyleValue['value']['data'] : $this->defaultBackgroundProps;
Expand All @@ -48,7 +53,7 @@ public function convert(array $variantStyle, array $mediaMapping = []): string
}

if (empty($styles)) {
return 'background: none;';
return '';
}

return join('', [
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/CssGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function testGenerate_WhenGivenWithBreakpoints_GeneratesBasedOnBreakpoint
$css = $generator->generate();

$expectedBreakpoint1 = '[data-widget-hash="random-hash"]:hover {color: rgb(0, 0, 0);}';
$expectedBreakpoint3 = '[data-widget-hash="random-hash"] {font-family: "Helvetica";}';
$expectedBreakpoint3 = '[data-widget-hash="random-hash"] {font-family: Helvetica;}';
$this->assertEquals($expectedBreakpoint1, $css[1]);
$this->assertEquals($expectedBreakpoint3, $css[3]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Strategies/BackgroundStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testConvert_WhenGivenSolidBackground_ReturnsGeneratedCss(): void
$backgroundStrategy = new BackgroundStrategy();
$css = $backgroundStrategy->convert($variantsStyles);

$expected = 'background: linear-gradient(rgba(253, 247, 237, 1), rgba(253, 247, 237, 1));background-size: auto;background-position: 0px 0px;background-repeat: no-repeat;background-attachment: scroll;';
$expected = 'background: rgba(253, 247, 237, 1);';
$this->assertEquals($expected, $css);
}

Expand All @@ -45,7 +45,7 @@ public function testConvert_WhenGivenSolidBackgroundAndActiveIsFalse_ReturnsNone
$backgroundStrategy = new BackgroundStrategy();
$css = $backgroundStrategy->convert($variantsStyles);

$expected = 'background: none;';
$expected = '';
$this->assertEquals($expected, $css);
}

Expand Down
14 changes: 0 additions & 14 deletions tests/Unit/Strategies/DefaultStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,4 @@ public function testConvert_SimpleCase_ReturnsPropertyValue(): void
$this->assertEquals($assertion[$index], $css);
}
}

public function testConvert_WhenGivenFontFamily_ReturnsValueWrappedWithQuote(): void
{
$variantsStyles = [
'type' => 'font-family',
'value' => "Something Fam'ily"
];

$defaultStrategy = new DefaultStrategy();

$css = $defaultStrategy->convert($variantsStyles);

$this->assertEquals('font-family: "Something Fam\'ily";', $css);
}
}

0 comments on commit 3e7878b

Please sign in to comment.