-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from studiometa/feature/custom-container-plugin
[Feature] Custom container plugin
- Loading branch information
Showing
9 changed files
with
210 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
sidebar: auto | ||
prev: ./grid.html | ||
next: ./variants-factory.html | ||
--- | ||
|
||
# Custom container | ||
|
||
The custom container plugin creates component classes `container` and `container-reset` to override the default [container core plugin](https://tailwindcss.com/docs/container). | ||
|
||
## Usage | ||
|
||
You can use all the Tailwind variants to control the display of the container. Here's an example with the [responsive variants](https://tailwindcss.com/docs/responsive-design) to enable the container only for the breakpoints `s, l, xl, xxl` and disable it for the breakpoints `xxs, xs and m`: | ||
|
||
```html | ||
<div class="s:container m:container-reset l:container"> | ||
... | ||
</div> | ||
``` | ||
|
||
## Configuration | ||
|
||
The [container core plugin](https://tailwindcss.com/docs/container) is disabled and the plugin can be configured with the `theme.customContainer` property: | ||
|
||
```js{3-7} | ||
module.exports = { | ||
theme: { | ||
customContainer: { | ||
center: true, | ||
padding: '1rem', | ||
maxWidth: '80rem', | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
::: tip | ||
You should set the custom container properties with the `theme.extend` property to not override these defaults. | ||
::: | ||
|
||
### `center` | ||
|
||
- Type: `Boolean` | ||
- Default: `true` | ||
|
||
Set right and left margins to `auto` if enabled. | ||
|
||
### `padding` | ||
|
||
- Type: `[ String, Object ]` | ||
- Default: `'1rem'` | ||
|
||
Set right and left paddings. You can use an object to make this setting responsive. ([Learn how](#responsive-configuration)) | ||
|
||
### `maxWidth` | ||
|
||
- Type: `[ String, Object ]` | ||
- Default: `'80rem'` | ||
|
||
Set the max width of the container. You can use an object to make this setting responsive. ([Learn how](#responsive-configuration)) | ||
|
||
## Responsive configuration | ||
|
||
`padding` and `maxWidth` properties are responsive if defined using objects like this: | ||
|
||
```js{5-12} | ||
module.exports = { | ||
theme: { | ||
extend: { | ||
customContainer: { | ||
padding: { | ||
DEFAULT: '1rem', | ||
m: '2rem', | ||
}, | ||
maxWidth: { | ||
DEFAULT: '80rem', | ||
xxl: '94rem', | ||
}, | ||
}, | ||
} | ||
}, | ||
}; | ||
``` | ||
|
||
## Use the container core plugin | ||
|
||
You need to disable the custom container plugin and enable the core plugin: | ||
|
||
```js{3,8-10} | ||
module.exports = { | ||
theme: { | ||
customContainer: false, | ||
container: { | ||
center: true, | ||
}, | ||
}, | ||
corePlugins: { | ||
container: true, | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
sidebar: auto | ||
prev: ./font-face.html | ||
next: ./variants-factory.html | ||
next: ./custom-container.html | ||
--- | ||
|
||
# Grid | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
sidebar: auto | ||
prev: ./grid.html | ||
prev: ./custom-container.html | ||
next: ./typography.html | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const plugin = require('tailwindcss/plugin'); | ||
|
||
/** | ||
* is Object ? | ||
* @param {*} value | ||
*/ | ||
const isObject = (value) => | ||
typeof value === 'object' && !!value && value.toString() === '[object Object]'; | ||
|
||
module.exports = plugin(({ addUtilities, theme }) => { | ||
const config = theme('customContainer'); | ||
if (!config) { | ||
return; | ||
} | ||
|
||
const allowedConfigProperties = [ | ||
'width', | ||
'maxWidth', | ||
'marginRight', | ||
'marginLeft', | ||
'paddingRight', | ||
'paddingLeft', | ||
]; | ||
|
||
const containerDefault = { | ||
width: '100%', | ||
}; | ||
|
||
const containerMediaQueries = {}; | ||
|
||
if (config.center) { | ||
config.marginRight = 'auto'; | ||
config.marginLeft = 'auto'; | ||
delete config.center; | ||
} | ||
|
||
if (config.padding) { | ||
config.paddingRight = config.padding; | ||
config.paddingLeft = config.padding; | ||
delete config.padding; | ||
} | ||
|
||
Object.entries(config) | ||
.filter(([propertyName]) => allowedConfigProperties.includes(propertyName)) | ||
.forEach(([propertyName, propertyValue]) => { | ||
if (!isObject(propertyValue)) { | ||
containerDefault[propertyName] = propertyValue; | ||
return; | ||
} | ||
|
||
Object.entries(propertyValue).forEach( | ||
([breakpointName, propertyValueForCurrentBreakpoint]) => { | ||
if (breakpointName === 'DEFAULT') { | ||
containerDefault[propertyName] = propertyValueForCurrentBreakpoint; | ||
return; | ||
} | ||
|
||
if (!containerMediaQueries[`@screen ${breakpointName}`]) { | ||
containerMediaQueries[`@screen ${breakpointName}`] = {}; | ||
} | ||
|
||
containerMediaQueries[`@screen ${breakpointName}`][propertyName] = | ||
propertyValueForCurrentBreakpoint; | ||
} | ||
); | ||
}); | ||
|
||
const container = { ...containerDefault, ...containerMediaQueries }; | ||
|
||
addUtilities({ | ||
'.container': container, | ||
'.container-reset': Object.fromEntries(Object.keys(container).map((key) => [key, 'unset'])), | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters