Skip to content

Commit

Permalink
WIP - First components
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Dec 14, 2024
1 parent 7918026 commit 317253c
Show file tree
Hide file tree
Showing 66 changed files with 339 additions and 74 deletions.
1 change: 1 addition & 0 deletions .stylelintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ dist
docs
node_modules
packages/registry/lib
build
7 changes: 7 additions & 0 deletions docs/source/configuration/settings-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,13 @@ querystringSearchGet
[See an explanation of character limits in URLs](https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers/417184#417184).
Please test this setting properly before enabling in a production site.
cssLayers
If you want to use CSS layers in Volto styling, it's possible to define them making sure that they are defined and applied at the very top level of the page (head tag).
By using this configuration, you can pass the layer list definition as an array:
```js
config.settings.cssLayers = ['reset', 'plone-components', 'layout', 'addons', 'theme'];
```
```

## Views settings
Expand Down
185 changes: 185 additions & 0 deletions packages/cmsui/components/Field/Field.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import config from '@plone/registry';
import type {
WidgetsConfigById,
WidgetsConfigByFactory,
WidgetsConfigByType,
WidgetsConfigByVocabulary,
WidgetsConfigByWidget,
} from '@plone/types';

type FieldProps = {
id: keyof WidgetsConfigById;
widget: keyof WidgetsConfigByWidget;
vocabulary: { '@id': keyof WidgetsConfigByVocabulary };
choices: string;
type: keyof WidgetsConfigByType;
focus: boolean;
mode: string;
widgetOptions: any;
factory: keyof WidgetsConfigByFactory;
onChange: (id: string, value: any) => void;
placeholder: string;
title: string;
};

const MODE_HIDDEN = 'hidden'; //hidden mode. If mode is hidden, field is not rendered

/**
* Get default widget
*/
const getWidgetDefault = (): React.ComponentType<any> => config.widgets.default;

/**
* Get widget by field's `id` attribute
*/
const getWidgetByFieldId = (
id: FieldProps['id'],
): React.ComponentType<any> | null => config.widgets.id[id] || null;

/**
* Get widget by factory attribute
*/
const getWidgetByFactory = (
factory: FieldProps['factory'],
): React.ComponentType<any> | null => config.widgets.factory?.[factory] || null;

/**
* Get widget by field's `widget` attribute
*/
const getWidgetByName = (
widget: FieldProps['widget'],
): React.ComponentType<any> | null =>
typeof widget === 'string'
? config.widgets.widget[widget] || getWidgetDefault()
: null;

/**
* Get widget by tagged values
*
directives.widget(
'fieldname',
frontendOptions={
"widget": 'specialwidget',
"version": 'extra'
})
*/
const getWidgetFromTaggedValues = (widgetOptions: {
frontendOptions: { widget: FieldProps['widget']; widgetProps: any };
}): React.ComponentType<any> | null =>
typeof widgetOptions?.frontendOptions?.widget === 'string'
? config.widgets.widget[widgetOptions.frontendOptions.widget]
: null;

/**
* Get widget props from tagged values
*
directives.widget(
"fieldname",
frontendOptions={
"widget": "specialwidget",
"widgetProps": {"prop1": "specialprop"}
})
*/
const getWidgetPropsFromTaggedValues = (widgetOptions: {
frontendOptions: { widget: string; widgetProps: any };
}): React.ComponentType<any> | null =>
typeof widgetOptions?.frontendOptions?.widgetProps === 'object'
? widgetOptions.frontendOptions.widgetProps
: null;

/**
* Get widget by field's `vocabulary` attribute
*/
const getWidgetByVocabulary = (
vocabulary: FieldProps['vocabulary'],
): React.ComponentType<any> | null =>
vocabulary && vocabulary['@id']
? config.widgets.vocabulary[
vocabulary['@id'].replace(
/^.*\/@vocabularies\//,
'',
) as keyof WidgetsConfigByVocabulary
]
: null;

/**
* Get widget by field's hints `vocabulary` attribute in widgetOptions
*/
const getWidgetByVocabularyFromHint = (
props: FieldProps,
): React.ComponentType<any> | null =>
props.widgetOptions && props.widgetOptions.vocabulary
? config.widgets.vocabulary[
props.widgetOptions.vocabulary['@id'].replace(
/^.*\/@vocabularies\//,
'',
) as keyof WidgetsConfigByVocabulary
]
: null;

/**
* Get widget by field's `choices` attribute
*/
const getWidgetByChoices = (
props: FieldProps,
): React.ComponentType<any> | null => {
if (props.choices) {
return config.widgets.choices;
}

if (props.vocabulary) {
// If vocabulary exists, then it means it's a choice field in disguise with
// no widget specified that probably contains a string then we force it
// to be a select widget instead
return config.widgets.choices;
}

return null;
};

/**
* Get widget by field's `type` attribute
*/
const getWidgetByType = (type: FieldProps['type']): React.ComponentType<any> =>
config.widgets.type[type] || null;

const Field = (props: FieldProps) => {
const Widget =
getWidgetByFieldId(props.id) ||
getWidgetFromTaggedValues(props.widgetOptions) ||
getWidgetByName(props.widget) ||
getWidgetByChoices(props) ||
getWidgetByVocabulary(props.vocabulary) ||
getWidgetByVocabularyFromHint(props) ||
getWidgetByFactory(props.factory) ||
getWidgetByType(props.type) ||
getWidgetDefault();

if (props.mode === MODE_HIDDEN) {
return null;
}

// Adding the widget props from tagged values (if any)
const widgetProps = {
...props,
label: props.title,
placeholder: props.placeholder || 'Type something...',
// Temporary translator from the old widget signature (id, value) to the new one (value)
onChange: (arg1: string, arg2: string | undefined) => {
if (!arg2 === undefined) {
props.onChange(props.id, arg1);
} else {
props.onChange(props.id, arg2);
}
},
...getWidgetPropsFromTaggedValues(props.widgetOptions),
};

return <Widget {...widgetProps} />;
};

export default Field;
9 changes: 9 additions & 0 deletions packages/cmsui/config/widgets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ConfigType } from '@plone/registry';
import { QuantaTextAreaField, QuantaTextField } from '@plone/components';

export default function install(config: ConfigType) {
config.widgets.default = QuantaTextField;
config.widgets.widget.textarea = QuantaTextAreaField;

return config;
}
4 changes: 4 additions & 0 deletions packages/cmsui/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { ConfigType } from '@plone/registry';
import installWidgets from './config/widgets';
import '@plone/components/dist/basic.css';
import '@plone/components/dist/quanta.css';

export default function install(config: ConfigType) {
installWidgets(config);
return config;
}
1 change: 1 addition & 0 deletions packages/cmsui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"main": "index.ts",
"scripts": {
"test": "vitest",
Expand Down
12 changes: 10 additions & 2 deletions packages/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ Using them as a baseline will allow you to quickly build your theme around them.
`@plone/components` basic styles provide a simple, yet powerful, set of tokenized custom CSS properties that will help you customize your own styles on the top of the basic styling.
You can override them in your classes while maintaining them for others.

### CSS layers

This package use CSS layers to provide a way to style the components in a more flexible way.
It uses the `plone-components` layer name to scope all the CSS declarations in the package.
The basic styling uses the nested `plone-components.base` named layer.
You can use the `plone-components` layer to override the basic styling, or use the `plone-components.base` layer to override the basic styling in a more specific way.

### Quanta

This package also features the Quanta components.
The Quanta theme is an example of it.
These components use the basic styling as a baseline, not only in styling, but also in the component side, reusing the CSS and custom CSS properties in it.
These components use the basic styling as a baseline, extending them to achieve Quanta look and feel.
They also extend the basic React components in a composable way.
The Quanta styling is scoped in the `plone-components.quanta` named layer.

Quanta is built upon the basic styles in an additive way.
The use of the Quanta CSS implies using it upon basic styling.
Expand Down
18 changes: 13 additions & 5 deletions packages/components/src/stories/Introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import { Meta } from '@storybook/blocks';

# `@plone/components`

This package contains ReactJS components for using Plone as a headless CMS.
This package contains ReactJS components for Plone 7 and Plone's API-first CMS story.

The purpose of this package is to provide an agnostic set of baseline components to build Plone sites upon.

## Usage

Using the package manager of your choice (npm, yarn, pnpm) install the package:
Using the package manager of your choice (npm, yarn, pnpm) to install the package in your app.
Use pnpm in case you are adding them a Volto add-on/workspace:

```shell
yarn add @plone/components
pnpm add @plone/components
```

Whenever you want to use the components, all are exported as named exports:
Expand Down Expand Up @@ -53,12 +54,19 @@ or selectively:
import '@plone/components/src/styles/basic/TextField.css';
```

### CSS layers

This package use CSS layers to provide a way to style the components in a more flexible way.
It uses the `plone-components` layer name to scope all the CSS declarations in the package.
The basic styling uses the nested `plone-components.base` named layer.
You can use the `plone-components` layer to override the basic styling, or use the `plone-components.base` layer to override the basic styling in a more specific way.

## Quanta

This package also features the Quanta components.
The Quanta theme is an example of it.
These components use the basic styling as a baseline, not only in styling, but also in the component side, reusing the CSS and custom CSS properties in it.
These components use the basic styling as a baseline, extending them to achieve Quanta look and feel.
They also extend the basic React components in a composable way.
The Quanta styling is scoped in the `plone-components.quanta` named layer.

Quanta is built upon the basic styles in an additive way.
The use of the Quanta CSS implies using it upon basic styling.
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/BlockToolbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@import './Menu.css';
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.blocks-toolbar {
display: flex;
flex-wrap: wrap;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/Breadcrumbs.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-Breadcrumbs {
display: flex;
align-items: center;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/Button.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-Button {
padding: 8px 8px;
border: 1px solid var(--border-color);
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/Calendar.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import './Button.css';
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-Calendar {
width: fit-content;
max-width: 100%;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/Checkbox.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-Checkbox {
--selected-color: var(--highlight-background);
--selected-color-pressed: var(--highlight-background-pressed);
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/CheckboxGroup.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@import './Button.css';
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-CheckboxGroup {
display: flex;
flex-direction: column;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/ColorArea.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import './ColorSlider.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-ColorArea {
width: 192px;
height: 192px;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/ColorField.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@import './Form.css';
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-ColorField {
display: flex;
flex-direction: column;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/ColorPicker.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@import './Select.css';
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.color-picker {
display: flex;
align-items: center;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/ColorSlider.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@layer plone-components {
@layer plone-components.base {
.react-aria-ColorSlider {
display: grid;
max-width: 300px;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/ColorSwatch.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import './ColorSlider.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-ColorSwatch {
width: 32px;
height: 32px;
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/styles/basic/ColorSwatchPicker.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@import './ColorField.css';
@import './theme.css';

@layer plone-components {
@layer plone-components.base {
.react-aria-ColorSwatchPicker {
display: flex;
flex-wrap: wrap;
Expand Down
Loading

0 comments on commit 317253c

Please sign in to comment.