Skip to content

Commit

Permalink
feat: add more flex layout support (#19)
Browse files Browse the repository at this point in the history
This PR changes the following:
- adds an fxFlexFill alternative
- adds an fxFlexAlign alternative
- adds flex-row properties to containers where it is missing and where
layout-align is used
- adds a documentation page on how to migrate from Flex Layout
- **renames `data-fx-flex` to `data-flex`** because it was the only
attribute selector with the `fx` prefix, which was confusing
- removes `data-fx-flex-grow` in favor of `data-flex="grow"`

It introduces breaking changes, but as we already introduced other
breaking changes for the next release this should be okay (as we're
releasing it as the next major version).
  • Loading branch information
philmtd authored Feb 24, 2023
1 parent 510b4fa commit 442611e
Show file tree
Hide file tree
Showing 23 changed files with 426 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ For example css-fx-layout provides attributes like the following, which can be c
* `data-layout` and `data-layout-align`
* `data-layout-gap`
* `data-hide-...` and `data-show-...`
* `data-fx-flex`
* `data-flex`

### CSS class selectors

Expand Down
2 changes: 1 addition & 1 deletion documentation/docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Using the data attributes is the easiest way to use this library and easier to m
* `data-layout` and `data-layout-align`: HTML attributes to be used to control the flexbox container
* `data-layout-gap`: attribute to add gaps between the elements in a flex container
* `data-hide-` and `data-show-`: attributes which can be used to show/hide elements depending on the screen size
* `data-fx-flex` and `data-fx-flex-grow`: Control flex behaviour of an element
* `data-flex`: Control flex behaviour of an element

### Responsive API

Expand Down
45 changes: 35 additions & 10 deletions documentation/docs/attribute-selectors/flex.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ sidebar_position: 3

# Flex API

This page describes how to use the attribute based flex API.
This page describes how to use the attribute based flex APIs and supports three selectors: `data-flex`, `data-flex-fill` and `data-flex-align`.

## `data-fx-flex` attribute
## `data-flex` attribute

The flex API consists of one attribute selector with the following available values and their resulting CSS:
The `data-flex` attribute can take the following values with their resulting CSS:

| Value | CSS |
|--------------|---------------------|
Expand All @@ -21,15 +21,15 @@ The flex API consists of one attribute selector with the following available val
| `nogrow` | `flex: 0 1 auto;` |
| `noshrink` | `flex: 1 0 auto;` |

## Support for flex sizes
### Support for flex sizes

Additionally, you can generate flex size selectors using the mixins as shown in the following example. They will extend the `data-fx-flex` attribute
Additionally, you can generate flex size selectors using the mixins as shown in the following example. They will extend the `data-flex` attribute
with numerical values, for example:

| Selector | CSS |
|---------------------------|------------------------|
| `data-fx-flex="<number>"` | `flex: 1 1 <number>%;` |
| e.g. `data-fx-flex="30"` | `flex: 1 1 30%;` |
| Selector | CSS |
|------------------------|------------------------|
| `data-flex="<number>"` | `flex: 1 1 <number>%;` |
| e.g. `data-flex="30"` | `flex: 1 1 30%;` |

This can be used to give elements a width with the percentage of the numerical value in relation to their flex container.

Expand All @@ -43,4 +43,29 @@ start and end and a given increment.

The second mixin will generate only selectors for the numbers passed as the first argument.

Both mixins can generate selectors for the responsive API (e.g. `data-fx-flex-gt-sm="50"`)
Both mixins can generate selectors for the responsive API (e.g. `data-flex-gt-sm="50"`)

## `data-flex-fill` attribute

The `data-flex-fill` attribute can be used to make a child of a flex-container fill the entire container.
It takes no arguments and adds the following properties to the element:

```css
margin: 0;
width: 100%;
height: 100%;
min-width: 100%;
min-height: 100%;
```

## `data-flex-align` attribute

The `data-flex-align` attribute takes the following values with their resulting CSS:

| Value | CSS |
|------------|---------------------------|
| `start` | `align-self: flex-start;` |
| `center` | `align-self: center;` |
| `end` | `align-self: flex-end;` |
| `baseline` | `align-self: baseline;` |
| `stretch` | `align-self: stretch;` |
4 changes: 3 additions & 1 deletion documentation/docs/attribute-selectors/responsive.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ The naming scheme is the following:
* `data-layout-[mediaQueryName]`
* `data-layout-align-[mediaQueryName]`
* `data-layout-gap-[mediaQueryName]`
* `data-fx-flex-[mediaQueryName]`
* `data-flex-[mediaQueryName]`
* `data-flex-fill-[mediaQueryName]`
* `data-flex-align-[mediaQueryName]`

With the media query name being for example `xs` or `lt-md`. See [this page](../responsive) for a full list.

Expand Down
9 changes: 9 additions & 0 deletions documentation/docs/attribute-selectors/show-hide.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ The attributes have the following naming scheme:

* `data-show-[mediaQueryName]`
* `data-hide-[mediaQueryName]`
* `data-hide`

With `mediaQueryName` being the name of one of the [available media queries](../responsive#media-queries).

:::caution Caution when using `data-show`

Due to the way `data-show` works, it can only be used on an element once and never in combination with `data-hide`. If you require fine-grained control over
when an element should be visible or not please only use `data-hide`! If you do not follow this advice your resulting layout will most likely not be
what you wanted.

:::

## Example

```html
Expand Down
34 changes: 30 additions & 4 deletions documentation/docs/class-selectors/flex.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ sidebar_position: 3

# Flex API

This page describes how to use the class based flex API.
This page describes how to use the class based flex APIs and supports three
types of class-selectors: `fx-flex`, `fx-flex-fill` and `fx-flex-align`.

## Selectors
## `fx-flex` classes

The flex API consists of classes with the following names and their resulting CSS:

Expand All @@ -21,7 +22,7 @@ The flex API consists of classes with the following names and their resulting CS
| `fx-flex-nogrow` | `flex: 0 1 auto;` |
| `fx-flex-noshrink` | `flex: 1 0 auto;` |

## Support for flex sizes
### Support for flex sizes

Additionally, you can generate flex size selectors using the mixins as shown in the following example. They will extend the `fx-flex` classes
with the following schema
Expand All @@ -43,4 +44,29 @@ start and end and a given increment.

The second mixin will generate only selectors for the numbers passed as the first argument.

Both mixins can generate selectors for the responsive API (e.g. `fx-flex--50--gt-sm`)
Both mixins can generate selectors for the responsive API (e.g. `fx-flex--50--gt-sm`)

## `fx-flex-fill` class

The `fx-flex-fill` class can be used to make a child of a flex-container fill the entire container.
It takes no arguments and adds the following properties to the element:

```css
margin: 0;
width: 100%;
height: 100%;
min-width: 100%;
min-height: 100%;
```

## `fx-flex-align` selectors

We offer the following flex align classes with their resulting CSS:

| Value | CSS |
|---------------------------|---------------------------|
| `fx-flex-align--start` | `align-self: flex-start;` |
| `fx-flex-align--center` | `align-self: center;` |
| `fx-flex-align--end` | `align-self: flex-end;` |
| `fx-flex-align--baseline` | `align-self: baseline;` |
| `fx-flex-align--stretch` | `align-self: stretch;` |
2 changes: 2 additions & 0 deletions documentation/docs/class-selectors/responsive.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ The naming scheme is the following:
* `fx-align--*-[crossAxis]--[mediaQueryName]`
* `fx-gap--[gapSize]--[mediaQueryName]`
* `fx-flex-[flex]--[mediaQueryName]`
* `fx-flex-fill--[mediaQueryName]`
* `fx-flex-align--[fillSelector]--[mediaQueryName]`

With the media query name being for example `xs` or `lt-md`. See [this page](../responsive) for a full list.

Expand Down
72 changes: 72 additions & 0 deletions documentation/docs/flex-layout-migration.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
sidebar_label: Migration from Angular Flex-Layout
sidebar_position: 6
---

# Migration from Angular Flex-Layout

If you consider migrating to css-fx-layout from Angular Flex-Layout be sure to know this library's limitations.
Not every feature of Flex-Layout is available but almost all popular features should be and some missing features can be
easily replaced.

As the migration is easier when you choose to use css-fx-layout with attribute selectors, this guide only focuses on this migration.

## Limitations

Let's start with what is missing in css-fx-layout in comparison with Angular Flex-Layout:

* no JavaScript API
* no general support for Angular expression bindings
* no alternative for `fxFlex` with numerical units other than percent
* the alternatives for `fxFlexAlign` only support `start`, `center`, `end`, `baseline` and `stretch`
* no grid functionality
* no alternative for `fxFlexOrder`
* no alternative for `fxFlexOffset`
* no custom breakpoints
* the alternative to `fxShow` only supports the responsive API and does not work the same way it does in Flex-Layout

:::warning Migrate away from `fxShow` / `data-show`

When moving to css-fx-layout it is recommended to migrate all elements using `fxShow` with more than one media size or in combination with `fxHide` to multiple `data-hide` attributes.
css-fx-layout is not a JavaScript powered library and cannot make the selectors work the same smart way they do in Angular Flex-Layout. Ignoring this advice and
keeping multiple `data-show` attributes on your elements or trying to combine them with `data-hide` will very likely not result in what you want!

:::

There *may* be more differences in the details but these are the most obvious and big ones.

## Migration

:::info

css-fx-layout generates some selectors only for the defined numerical values, for example the gap or flex selectors.
Make sure to customise the parameters of the mixins in a way that generates all selectors you need in your project.

:::

Now that we got this out of the way let's migrate. Most of it should be possible to do with some smart
finds and replaces in your code using regular expressions.

| Flex-Layout selector | Regex search | Regex replace |
|----------------------------|----------------------------|-------------------------|
| `fxLayout` | `fxLayout=` | `data-layout=` |
| `fxLayout` responsive | `fxLayout\.([\w-]+)=` | `data-layout-$1=` |
| `fxLayoutAlign` | `fxLayoutAlign=` | `data-layout-align=` |
| `fxLayoutAlign` responsive | `fxLayoutAlign\.([\w-]+)=` | `data-layout-align-$1=` |
| `fxFlex` | `fxFlex=` | `data-flex=` |
| `fxFlex` responsive | `fxFlex\.([\w-]+)=` | `data-flex-$1=` |
| `fxLayoutGap` | `fxLayoutGap=` | `data-layout-gap=` |
| `fxLayoutGap` responsive | `fxLayoutGap\.([\w-]+)=` | `data-layout-gap-$1=` |
| `fxHide` responsive | `fxHide\.([\w-]+)` | `data-hide-$1` |
| `fxShow` responsive | `fxShow\.([\w-]+)` | `data-show-$1` |
| `fxFlexFill` | `fxFlexFill` | `data-flex-fill` |
| `fxFlexFill` responsive | `fxFlexFill\.([\w-]+)` | `data-flex-fill-$1` |

Additionally:
* you need to replace all occurrences of `row-reverse` and `column-reverse` with `row reverse` or `column reverse`
* you should change your code to follow the advice regarding `data-show` from the warning box above

This should get the migration of all available features done. In many projects, even larger ones, this can already be 80-100% of the work required
to move away from Angular Flex-Layout.

Did we miss something? Feel free to contribute to the documentation or the library on [GitHub](https://github.com/philmtd/css-fx-layout).
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ The attributes have the following naming scheme:

With `mediaQueryName` being the name of one of the [available media queries](../responsive#media-queries).

:::caution

Due to the way `data-show` works it can only be used on an element once and not in combination with `data-hide`. If you require fine-grained control over
when an element should be visible or not please only use `data-hide`!

:::

## Example

```html
Expand Down
6 changes: 6 additions & 0 deletions src/fxlayout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@use "lib/flex-classes" as flexC;
@use "lib/flex-size-attributes" as flexSizeA;
@use "lib/flex-size-classes" as flexSizeC;
@use "lib/flex-align-attributes" as flexAlignA;
@use "lib/flex-align-classes" as flexAlignC;

/// Adds the css-fx-layout class selectors, e.g. `.fx-layout-row`
///
Expand All @@ -20,11 +22,13 @@
@include layoutC.layout-classes;
@include layoutAlignC.layout-align-classes;
@include flexC.flex-classes;
@include flexAlignC.flex-align-classes;

@if $includeSelectorsForMediaSizes {
@include layoutC.layout-classes-for-media-sizes;
@include layoutAlignC.layout-align-classes-for-media-sizes;
@include flexC.flex-classes-for-media-sizes;
@include flexAlignC.flex-align-classes-for-media-sizes;
}
}

Expand Down Expand Up @@ -89,11 +93,13 @@
@include layoutA.layout-attributes;
@include layoutAlignA.layout-align-attributes;
@include flexA.flex-attributes;
@include flexAlignA.flex-align-attributes;

@if $includeSelectorsForMediaSizes {
@include layoutA.layout-attributes-for-media-sizes;
@include layoutAlignA.layout-align-attributes-for-media-sizes;
@include flexA.flex-attributes-for-media-sizes;
@include flexAlignA.flex-align-attributes-for-media-sizes;
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/lib/flex-align-attributes.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@import "mixins";

/// Generates flex align data attribute selectors
@mixin flex-align-attributes {
*[data-flex-align="start"] {
@include flex-align-start-properties;
}
*[data-flex-align="center"] {
@include flex-align-center-properties;
}
*[data-flex-align="end"] {
@include flex-align-end-properties;
}
*[data-flex-align="baseline"] {
@include flex-align-baseline-properties;
}
*[data-flex-align="stretch"] {
@include flex-align-stretch-properties;
}
}

@mixin flex-align-attributes-for-media-sizes {
@each $name, $_ in $flex-layout-media-queries {
@include flex-layout-media($name) {
*[data-flex-align-#{$name}="start"] {
@include flex-align-start-properties;
}
*[data-flex-align-#{$name}="center"] {
@include flex-align-center-properties;
}
*[data-flex-align-#{$name}="end"] {
@include flex-align-end-properties;
}
*[data-flex-align-#{$name}="baseline"] {
@include flex-align-baseline-properties;
}
*[data-flex-align-#{$name}="stretch"] {
@include flex-align-stretch-properties;
}
}
}
}
42 changes: 42 additions & 0 deletions src/lib/flex-align-classes.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@import "mixins";

/// Generates flex align class selectors
@mixin flex-align-classes {
*.fx-flex-align--start {
@include flex-align-start-properties;
}
*.fx-flex-align--center {
@include flex-align-center-properties;
}
*.fx-flex-align--end {
@include flex-align-end-properties;
}
*.fx-flex-align--baseline {
@include flex-align-baseline-properties;
}
*.fx-flex-align--stretch {
@include flex-align-stretch-properties;
}
}

@mixin flex-align-classes-for-media-sizes {
@each $name, $_ in $flex-layout-media-queries {
@include flex-layout-media($name) {
*.fx-flex-align--start--#{$name} {
@include flex-align-start-properties;
}
*.fx-flex-align--center--#{$name} {
@include flex-align-center-properties;
}
*.fx-flex-align--end--#{$name} {
@include flex-align-end-properties;
}
*.fx-flex-align--baseline--#{$name} {
@include flex-align-baseline-properties;
}
*.fx-flex-align--stretch--#{$name} {
@include flex-align-stretch-properties;
}
}
}
}
Loading

0 comments on commit 442611e

Please sign in to comment.