Skip to content

Commit

Permalink
feat(forms): include styles by default (#2918)
Browse files Browse the repository at this point in the history
This PR includes the forms extension styles in the components package.
This means, that theses styles do not need to be imported separately.

These styles are very small in size (6 KB), and will not grow
substantially either.

It also allows automated generation of theme files, when needed.
  • Loading branch information
tujoworker authored Nov 20, 2023
1 parent b2dbd44 commit e2f4c1e
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ import FormDiagram2 from 'Docs/uilib/extensions/forms/form-diagram-2.png'
Field components can be used directly as they are, for example `Field.Email`:

```jsx
import '@dnb/eufemia/extensions/forms/style'
import { Field } from '@dnb/eufemia/extensions/forms'
render(<Field.Email />)
```

By building an entire form with components from Eufemia and Eufemia Forms, you save time and code:

```jsx
import '@dnb/eufemia/extensions/forms/style'
import { Card } from '@dnb/eufemia'
import { Form, Field } from '@dnb/eufemia/extensions/forms'
render(
Expand Down Expand Up @@ -130,12 +128,23 @@ In this example, all state data, validation process and error handling are done

## Getting started

You need to import the styles once:
You import the components from with scopes, such as `Form` and `Field`:

```jsx
import '@dnb/eufemia/extensions/forms/style'
import { Form, Field } from '@dnb/eufemia/extensions/forms'

render(
<Form.Handler onSubmit={console.log}>
<Field.String label="Custom Field" />
<Form.ButtonRow>
<Form.SubmitButton />
</Form.ButtonRow>
</Form.Handler>,
)
```

The needed styles are included in the Eufemia core package via `dnb-ui-components`.

### Field components

The data-driven [base field components](/uilib/extensions/forms/base-fields) are named and structured according to the type of data they can display and produce based on the user's input and action in the interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ The `Form.Handler` component provides both the [Form.Element](/uilib/extensions/
- It calls `preventDefault` by default.

```jsx
import '@dnb/eufemia/extensions/forms/style'
import { Form } from '@dnb/eufemia/extensions/forms'
render(
<Form.Handler
Expand Down
23 changes: 23 additions & 0 deletions packages/dnb-eufemia/scripts/prebuild/tasks/styleFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async function runStyleFactory() {
customContent: `
@import './core/utilities.scss';
@import './${fallbackPrefix}-ui-fragments.scss';
@import './${fallbackPrefix}-ui-forms.scss';
`,
importContent: ({ relativeSource }) => `@import '${relativeSource}';`,
searchGlob: [
Expand Down Expand Up @@ -123,6 +124,28 @@ async function runStyleFactory() {
log.info('> Created the style file with all the extensions')
}
})

// forms
await runFactory({
outputFile: path.resolve(
__dirname,
`../../../src/style/${fallbackPrefix}-ui-forms.scss`
),
customContent: `@import './core/utilities.scss';`,
importContent: ({ relativeSource }) => `@import '${relativeSource}';`,
searchGlob: [
path.resolve(
__dirname,
`../../../src/extensions/forms/**/style/${fallbackPrefix}-*.scss`
),
].concat(['!**/style/dnb-forms.*']),
}).then(() => {
if (isCLI) {
log.succeed(
'> PrePublish: "styleFactory" Created the style file with all the forms styles'
)
}
})
}

const runFactory = async ({
Expand Down
22 changes: 22 additions & 0 deletions packages/dnb-eufemia/scripts/prebuild/tasks/themeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async function runThemeFactory() {
),
...processToNamesIgnoreList,
],
customContent: ({ name }) => `@import './${name}-theme-forms.scss';`,
// output
targetFile: 'components', // ui-theme-components.scss
scssOutputPath: path.resolve(__dirname, '../../../src/style/themes'),
Expand Down Expand Up @@ -131,6 +132,27 @@ async function runThemeFactory() {
)
}
})

// forms
await runFactory({
// input
filesToFindGlob: [
path.resolve(
__dirname,
'../../../src/extensions/forms/**/style/themes/**/*-theme-*.scss'
),
...processToNamesIgnoreList,
].concat(['!**/forms/style/themes/*']),
// output
targetFile: 'forms', // ui-theme-forms.scss
scssOutputPath: path.resolve(__dirname, '../../../src/style/themes'),
}).then(() => {
if (isCLI) {
log.succeed(
'> PrePublish: "themeFactory" Created the themes files with all the components, elements, fragments and extensions'
)
}
})
}

export async function runFactory({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@import '../../style/field-sizes.scss';

.dnb-forms-field-array-selection {
&--options-layout-vertical &__options {
display: flex;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@import '../../style/field-sizes.scss';

.dnb-forms-field-expiry {
.dnb-multi-input-mask {
width: var(--forms-field-width--small);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
@import '../../style/field-sizes.scss';

.dnb-forms-field-phone-number {
&__country-code {
width: 9rem;
width: calc(var(--forms-field-width--medium) - 2rem);

.dnb-autocomplete__shell {
width: 9rem;
width: calc(var(--forms-field-width--medium) - 2rem);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@import '../../style/field-sizes.scss';

.dnb-forms-field-selection {
&--width {
&-large {
Expand Down

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions packages/dnb-eufemia/src/extensions/forms/Form/style/index.ts

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
@import '../Form/style/dnb-forms-form.scss';
@import '../Field/style/dnb-forms-field.scss';
@import '../StepsLayout/style/index.scss';
@import '../utils/TestElement/style/dnb-test-element.scss';
@import '../FieldBlock/style/dnb-field-block.scss';
@import '../ValueBlock/style/dnb-value-block.scss';
@import '../../../style/dnb-ui-forms.scss';
6 changes: 0 additions & 6 deletions packages/dnb-eufemia/src/extensions/forms/style/index.js

This file was deleted.

6 changes: 0 additions & 6 deletions packages/dnb-eufemia/src/extensions/style/index.js

This file was deleted.

13 changes: 13 additions & 0 deletions packages/dnb-eufemia/src/extensions/style/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Web Style Import
*
*/

import '../../style/themes/theme-ui/ui-theme-extensions.scss'

import { warn } from '../../shared/helpers'

// @deprecated - this warning should be remove when forms is out of beta
warn(
"This import statement is not needed and can be removed: import '@dnb/eufemia/extensions/forms/style'"
)
1 change: 1 addition & 0 deletions packages/dnb-eufemia/src/style/dnb-ui-components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

@import './core/utilities.scss';
@import './dnb-ui-fragments.scss';
@import './dnb-ui-forms.scss';
@import '../components/accordion/style/dnb-accordion.scss';
@import '../components/anchor/style/dnb-anchor.scss';
@import '../components/autocomplete/style/dnb-autocomplete.scss';
Expand Down
19 changes: 19 additions & 0 deletions packages/dnb-eufemia/src/style/dnb-ui-forms.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* ATTENTION: This file is auto generated by using "styleFactory".
* Do not change the content!
*
*/

@import './core/utilities.scss';
@import '../extensions/forms/Field/ArraySelection/style/dnb-array-selection.scss';
@import '../extensions/forms/Field/Expiry/style/dnb-expiry.scss';
@import '../extensions/forms/Field/PhoneNumber/style/dnb-phone-number.scss';
@import '../extensions/forms/Field/PostalCodeAndCity/style/dnb-postal-code-and-city.scss';
@import '../extensions/forms/Field/Selection/style/dnb-selection.scss';
@import '../extensions/forms/FieldBlock/style/dnb-field-block.scss';
@import '../extensions/forms/Form/ButtonRow/style/dnb-form-button-row.scss';
@import '../extensions/forms/Form/MainHeading/style/dnb-form-main-heading.scss';
@import '../extensions/forms/Form/SubHeading/style/dnb-form-sub-heading.scss';
@import '../extensions/forms/StepsLayout/style/dnb-steps-layout.scss';
@import '../extensions/forms/ValueBlock/style/dnb-value-block.scss';
@import '../extensions/forms/utils/TestElement/style/dnb-test-element.scss';
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ $fonts-path: '../../../../assets/fonts/dnb' !default;
@import '../../../components/upload/style/themes/dnb-upload-theme-ui.scss';
@import '../../../fragments/drawer-list/style/themes/dnb-drawer-list-theme-ui.scss';
@import '../../../fragments/scroll-view/style/themes/dnb-scroll-view-theme-ui.scss';
@import './eiendom-theme-forms.scss';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* ATTENTION: This file is auto generated by using "themeFactory".
* But you still can change the content of this file on the very top.
*/

// Add "ui" themes, if file don't exists
$THEME_FALLBACK: 'ui';

// Import shared styles
@import '../../dnb-ui-forms.scss';

/**
* NB: The content below is auto generated by the "themeFactory".
* You may want to update it by running "yarn build" locally.
*/
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ $fonts-path: '../../../../assets/fonts/dnb' !default;
@import '../../../components/timeline/style/themes/dnb-timeline-theme-ui.scss';
@import '../../../components/toggle-button/style/themes/dnb-toggle-button-theme-ui.scss';
@import '../../../fragments/scroll-view/style/themes/dnb-scroll-view-theme-ui.scss';
@import './sbanken-theme-forms.scss';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* ATTENTION: This file is auto generated by using "themeFactory".
* But you still can change the content of this file on the very top.
*/

// Add "ui" themes, if file don't exists
$THEME_FALLBACK: 'ui';

// Import shared styles
@import '../../dnb-ui-forms.scss';

/**
* NB: The content below is auto generated by the "themeFactory".
* You may want to update it by running "yarn build" locally.
*/
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ $fonts-path: '../../../../assets/fonts/dnb' !default;
@import '../../../components/upload/style/themes/dnb-upload-theme-ui.scss';
@import '../../../fragments/drawer-list/style/themes/dnb-drawer-list-theme-ui.scss';
@import '../../../fragments/scroll-view/style/themes/dnb-scroll-view-theme-ui.scss';
@import './ui-theme-forms.scss';
15 changes: 15 additions & 0 deletions packages/dnb-eufemia/src/style/themes/theme-ui/ui-theme-forms.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* ATTENTION: This file is auto generated by using "themeFactory".
* But you still can change the content of this file on the very top.
*/

// Add "ui" themes, if file don't exists
$THEME_FALLBACK: 'ui';

// Import shared styles
@import '../../dnb-ui-forms.scss';

/**
* NB: The content below is auto generated by the "themeFactory".
* You may want to update it by running "yarn build" locally.
*/

0 comments on commit e2f4c1e

Please sign in to comment.