Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Shadow DOM for the CreateGroupForm app #8793

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 15 additions & 41 deletions h/static/scripts/group-forms/components/CreateGroupForm.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,29 @@
import { Button, Input, Textarea } from '@hypothesis/frontend-shared';

export default function CreateGroupForm() {
return (
<>
<h1 class="form-header">Create a new private group</h1>
<h1>Create a new private group</h1>

<form class="form group-form__form">
<div class="form-input">
<label for="name" class="form-input__label group-form__name-label">
Name <span class="form-input__required">*</span>
</label>
<input
type="text"
id="name"
class="form-input__input group-form__name-input has-label"
autofocus
autocomplete="off"
required
/>
<span class="form-input__character-counter is-ready">0/25</span>
<form>
<div>
<label for="name">Name*</label>
<Input id="name" autofocus autocomplete="off" required />
0/25
</div>

<div class="form-input">
<label
class="form-input__label group-form__description-label"
for="description"
>
Description
</label>
<textarea
id="description"
class="form-input__input group-form__description-input has-label"
></textarea>
<span class="form-input__character-counter is-ready">0/250</span>
<div>
<label for="description">Description</label>
<Textarea id="description" />
0/250
</div>

<div class="form-actions">
<div class="u-stretch"></div>

<div class="form-actions__buttons">
<button class="form-actions__btn btn primary-action-btn group-form__submit-btn">
Create group
</button>
</div>
<div>
<Button variant="primary">Create group</Button>
</div>
</form>

<footer class="form-footer">
<span class="form-footer__required">
<span class="form-footer__symbol">*</span>
<span class="form-footer__text">Required</span>
</span>
</footer>
<footer>*Required</footer>
</>
);
}
9 changes: 9 additions & 0 deletions h/static/scripts/group-forms/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type ConfigObject = {
/** The URLs of the app's CSS stylesheets. */
styles: string[];
};

/** Return the frontend config from the page's <script class="js-config">. */
export default function readConfig(): ConfigObject {
return JSON.parse(document.querySelector('.js-config')!.textContent!);
}
15 changes: 14 additions & 1 deletion h/static/scripts/group-forms/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { render } from 'preact';

import CreateGroupForm from './components/CreateGroupForm';
import readConfig from './config';

function init() {
render(<CreateGroupForm />, document.querySelector('#create-group-form')!);
const shadowHost = document.querySelector('#create-group-form')!;
const shadowRoot = shadowHost.attachShadow({ mode: 'open' });
const config = readConfig();
const stylesheetLinks = config.styles.map(stylesheetURL => (
<link rel="stylesheet" href={stylesheetURL} />
Dismissed Show dismissed Hide dismissed
));
render(
<>
{stylesheetLinks}
<CreateGroupForm />
</>,
shadowRoot,
);
}

init();
13 changes: 5 additions & 8 deletions h/templates/groups/create.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

{% set page_title = 'Create a new private group' %}

{% block styles %}
{{ super() }}

{% for url in asset_urls('group_forms_css') %}
<link rel="stylesheet" href="{{ url }}">
{% endfor %}
{% endblock %}

{% block page_content %}
<div id="create-group-form"></div>
<script type="application/json" class="js-config">
{
"styles": {{ asset_urls("group_forms_css")|tojson }}
}
</script>
{% endblock %}

{% block scripts %}
Expand Down
Loading