Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit c64c430

Browse files
author
Mikhail Aheichyk
committed
Wrapper React Component replacing Banner.
Signed-off-by: Mikhail Aheichyk <[email protected]>
1 parent 220c551 commit c64c430

File tree

9 files changed

+626
-103
lines changed

9 files changed

+626
-103
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ being raised for additional handling.
9898
The module can also change what room/user/entity the user is looking at, and join it (if it's a room), with
9999
`navigatePermalink` on a `ModuleApi` instance.
100100

101-
### Banner management
102-
From the `RuntimeModule` instance, modules can listen for `BannerLifecycle.Banner` to provide a banner. It will be
103-
shown at the top of the Element.
101+
### Wrapper management
102+
From the `RuntimeModule` instance, modules can listen for `WrapperLifecycle.Wrapper` to provide a wrapper react component.
103+
It would wrap the `MatrixChat` component and let any consumer add a banner, a footer.
104104

105105
## Contributing / developing
106106

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
22
module.exports = {
33
preset: 'ts-jest',
4-
testEnvironment: 'node',
5-
};
4+
testEnvironment: 'jsdom',
5+
};

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
"@babel/preset-env": "^7.16.11",
3636
"@babel/preset-react": "^7.16.7",
3737
"@babel/preset-typescript": "^7.16.7",
38+
"@testing-library/jest-dom": "^5.16.5",
39+
"@testing-library/react": "^12.1.5",
3840
"@types/jest": "^27.4.1",
3941
"@types/react": "^17",
42+
"@types/react-dom": "^17.0.19",
4043
"@typescript-eslint/eslint-plugin": "^5.18.0",
4144
"@typescript-eslint/parser": "^5.18.0",
4245
"eslint": "^8.12.0",
@@ -45,6 +48,7 @@
4548
"eslint-plugin-matrix-org": "^0.4.0",
4649
"jest": "^27.5.1",
4750
"react": "17.0.2",
51+
"react-dom": "^17.0.2",
4852
"rimraf": "^3.0.2",
4953
"ts-jest": "^27.1.4",
5054
"typescript": "^4.6.3"

src/lifecycles/BannerLifecycle.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/lifecycles/WrapperLifecycle.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ComponentType, PropsWithChildren } from 'react';
2+
3+
/**
4+
* Wrapper lifecycle events
5+
*/
6+
export enum WrapperLifecycle {
7+
/**
8+
* An event to request the wrapper. It is sent by Element to get the wrapper provided by the module if any.
9+
*/
10+
Wrapper = 'wrapper'
11+
}
12+
13+
/**
14+
* Opts object that is populated with a Wrapper.
15+
*/
16+
export type WrapperOpts = {
17+
/**
18+
* A Wrapper React Component to be rendered around the Matrix Chat.
19+
*/
20+
Wrapper: ComponentType<PropsWithChildren<{}>>;
21+
};
22+
23+
/**
24+
* Helper type that documents how to implement a wrapper listener.
25+
*/
26+
export type WrapperListener = (opts: WrapperOpts) => void;

src/lifecycles/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ limitations under the License.
1616

1717
import { RoomViewLifecycle } from "./RoomViewLifecycle";
1818
import { WidgetLifecycle } from "./WidgetLifecycle";
19-
import { BannerLifecycle } from "./BannerLifecycle";
19+
import { WrapperLifecycle } from "./WrapperLifecycle";
2020

2121
export type AnyLifecycle =
2222
| RoomViewLifecycle
2323
| WidgetLifecycle
24-
| BannerLifecycle
24+
| WrapperLifecycle
2525
;

test/lifecycles/BannerLifecycle.test.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2023 Mikhail Aheichyk
3+
Copyright 2023 Nordeck IT + Consulting GmbH.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
import React from 'react';
18+
import { render, screen } from '@testing-library/react';
19+
20+
import { RuntimeModule } from '../../src/RuntimeModule';
21+
import { WrapperLifecycle, WrapperListener, WrapperOpts } from '../../src/lifecycles/WrapperLifecycle';
22+
23+
describe('WrapperLifecycle', () => {
24+
let module: RuntimeModule;
25+
26+
beforeAll(() => {
27+
module = new class extends RuntimeModule {
28+
constructor() {
29+
super(undefined as any);
30+
31+
this.on(WrapperLifecycle.Wrapper, this.wrapperListener);
32+
}
33+
34+
protected wrapperListener: WrapperListener = (wrapperOpts: WrapperOpts) => {
35+
wrapperOpts.Wrapper = ({ children }) => {
36+
return <><div>Header</div>{children}<div>Footer</div></>;
37+
};
38+
};
39+
};
40+
});
41+
42+
it('should wrap a matrix client with header and footer', () => {
43+
const opts: WrapperOpts = { Wrapper: React.Fragment };
44+
module.emit(WrapperLifecycle.Wrapper, opts);
45+
46+
render(<opts.Wrapper><span>MatrixChat</span></opts.Wrapper>);
47+
screen.getByText(/Header/i);
48+
screen.getByText(/MatrixChat/i);
49+
screen.getByText(/Footer/i);
50+
});
51+
});
52+

0 commit comments

Comments
 (0)