Skip to content

Commit

Permalink
Merge pull request #143 from xiCO2k/feat/min-w
Browse files Browse the repository at this point in the history
feat: Add `min-w-{width}` class.
  • Loading branch information
xiCO2k authored Jul 1, 2022
2 parents bf104ba + 1c5e4b4 commit 731142f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ All the classes supported use exactly the same logic that is available on [tailw
* **[Padding](https://tailwindcss.com/docs/padding):** `p-{padding}`, `pl-{leftPadding}`, `pr-{rightPadding}`, `pt-{topPadding}`, `pb-{bottomPadding}`, `px-{horizontalPadding}`, `py-{verticalPadding}`.
* **[Space](https://tailwindcss.com/docs/space):** `space-y-{space}`, `space-x-{space}`.
* **[Width](https://tailwindcss.com/docs/width):** `w-{width}`, `w-full`
* **[Min Width](https://tailwindcss.com/docs/min-width):** `min-w-{width}`
* **[Max Width](https://tailwindcss.com/docs/max-width):** `max-w-{width}`
* **[Justify Content](https://tailwindcss.com/docs/justify-content):** `justify-between`, `justify-around`, `justify-evenly`, `justify-center`
* **[Visibility](https://tailwindcss.com/docs/visibility):** `invisible`
Expand Down
18 changes: 16 additions & 2 deletions src/ValueObjects/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ final public function addStyle(string $style): self
*/
final public function inheritFromStyles(self $styles): self
{
foreach (['ml', 'mr', 'pl', 'pr', 'width', 'maxWidth', 'spaceY', 'spaceX'] as $style) {
foreach (['ml', 'mr', 'pl', 'pr', 'width', 'minWidth', 'maxWidth', 'spaceY', 'spaceX'] as $style) {
$this->properties['parentStyles'][$style] = array_merge(
$this->properties['parentStyles'][$style] ?? [],
$styles->properties['parentStyles'][$style] ?? []
Expand Down Expand Up @@ -450,6 +450,16 @@ final public function wFull(): static
return $this->w('1/1');
}

/**
* Defines a minimum width of an element.
*/
final public function minW(int|string $width): static
{
return $this->with(['styles' => [
'minWidth' => $width,
]]);
}

/**
* Defines a maximum width of an element.
*/
Expand Down Expand Up @@ -792,7 +802,8 @@ private function getPaddings(): array
private function applyWidth(string $content): string
{
$styles = $this->properties['styles'] ?? [];
$width = $styles['width'] ?? -1;
$minWidth = $styles['minWidth'] ?? -1;
$width = max($styles['width'] ?? -1, $minWidth);
$maxWidth = $styles['maxWidth'] ?? 0;

if ($width < 0) {
Expand Down Expand Up @@ -979,9 +990,12 @@ public static function getParentWidth(array $styles): int
{
$width = terminal()->width();
foreach ($styles['width'] ?? [] as $index => $parentWidth) {
$minWidth = (int) $styles['minWidth'][$index];
$maxWidth = (int) $styles['maxWidth'][$index];
$margins = (int) $styles['ml'][$index] + (int) $styles['mr'][$index];

$parentWidth = max($parentWidth, $minWidth);

if ($parentWidth < 1) {
$parentWidth = $width;
} elseif (is_int($parentWidth)) {
Expand Down
13 changes: 13 additions & 0 deletions tests/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@
->toThrow(InvalidStyle::class);
});

test('min-w', function () {
putenv('COLUMNS=10');

$html = parse(<<<'HTML'
<div class="flex">
<span class="flex-1 content-repeat-[.] min-w-1"></span>
<span>Over size content</span>
</div>
HTML);

expect($html)->toBe('.Over size content');
});

test('max-w', function () {
putenv('COLUMNS=10');

Expand Down

0 comments on commit 731142f

Please # to comment.