diff --git a/README.md b/README.md index edb2aae..9798020 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/src/ValueObjects/Styles.php b/src/ValueObjects/Styles.php index b41b3ed..22a3252 100644 --- a/src/ValueObjects/Styles.php +++ b/src/ValueObjects/Styles.php @@ -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] ?? [] @@ -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. */ @@ -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) { @@ -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)) { diff --git a/tests/classes.php b/tests/classes.php index 7c921b9..e2bcdae 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -205,6 +205,19 @@ ->toThrow(InvalidStyle::class); }); +test('min-w', function () { + putenv('COLUMNS=10'); + + $html = parse(<<<'HTML' +
+ + Over size content +
+ HTML); + + expect($html)->toBe('.Over size content'); +}); + test('max-w', function () { putenv('COLUMNS=10');