*/ public static function compile_size(string $prop, $size, array $ctx = []): array { if (!is_array($size) || !Value::get($size, 'mode')) { return []; } $out = []; $axis = $prop === 'width' ? 'x' : 'y'; $main = self::main_axis($ctx['parentStyle'] ?? null); $is_main_axis = $main === $axis; $is_absolute = !empty($ctx['isAbsolute']); $in_flex = $main !== null && !$is_absolute; $min_prop = $axis === 'x' ? 'min-width' : 'min-height'; $max_prop = $axis === 'x' ? 'max-width' : 'max-height'; switch (Value::get($size, 'mode')) { case 'fixed': case 'relative': $out[$prop] = self::length_of($size); if ($in_flex && $is_main_axis) { $out['flex'] = '0 0 ' . self::length_of($size); } break; case 'fill': if ($is_absolute) { $out[$prop] = '100%'; } elseif ($in_flex && $is_main_axis) { $out['flex'] = '1 1 0%'; $out[$min_prop] = '0'; } elseif ($in_flex) { if (Value::get($size, 'max') !== null) { $out[$prop] = '100%'; } else { $out['align-self'] = 'stretch'; $out[$prop] = 'auto'; } } else { /* The parent is not a stack. */ $out[$prop] = '100%'; } break; case 'fit': if ($in_flex && $is_main_axis) { $out['flex'] = '0 0 auto'; $out[$prop] = 'auto'; } else { $out[$prop] = 'fit-content'; } break; } $unit = Value::get($size, 'unit'); $with_unit = static function ($v) use ($unit) { return ($unit && (is_int($v) || is_float($v))) ? Value::str($v) . $unit : Value::px($v); }; if (Value::get($size, 'min') !== null) { $out[$min_prop] = $with_unit($size['min']); } if (Value::get($size, 'max') !== null) { $out[$max_prop] = $with_unit($size['max']); } return $out; } /** @return array */ public static function compile_position($position): array { if (!is_array($position) || $position === []) { return ['position' => 'relative']; } $mode = Value::get($position, 'mode'); $pins = Value::get($position, 'pins'); $pins = is_array($pins) ? $pins : []; $z = Value::get($position, 'zIndex'); if ($mode === 'sticky') { $out = ['position' => 'sticky']; $pinned = false; foreach (['top', 'bottom', 'left', 'right'] as $side) { if (Value::get($pins, $side) !== null) { $out[$side] = Value::px($pins[$side]); $pinned = true; } } if (!$pinned) { $out['top'] = '0px'; } if ($z !== null) { $out['z-index'] = Value::str($z); } return $out; } if ($mode !== 'absolute' && $mode !== 'fixed') { $flow = ['position' => 'relative']; if ($z !== null) { $flow['z-index'] = Value::str($z); } return $flow; } $out = ['position' => $mode]; foreach (['left', 'top', 'right', 'bottom'] as $side) { if (Value::get($pins, $side) !== null) { $out[$side] = Value::px($pins[$side]); } } if ($z !== null) { $out['z-index'] = Value::str($z); } $center = Value::get($position, 'center'); $center = is_array($center) ? $center : []; $tf = []; if (!empty($center['x'])) { $out['left'] = '50%'; $tf[] = 'translateX(-50%)'; } if (!empty($center['y'])) { $out['top'] = '50%'; $tf[] = 'translateY(-50%)'; } if ($tf) { /* Read back by the compiler and merged into `transform`. */ $out['--af-center'] = implode(' ', $tf); } return $out; } }