Introduction
react-live-clock is an react plugin to show a digital clock. In this article we will leverage this plugin as an extension to make this visible in the entire site.
Steps
Open a command prompt and create a directory for the SPFx solution.
md spfx-ReactClockExtension
Navigate to the above-created directory.
cd spfx-ReactClockExtension
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter for the default name (spfx-ReactClockExtension in this case) or type in any other name for your solution.
Selected choice - Hit Enter
Target for the component
Here,
we can select the target environment where we are planning to deploy
the client web part; i.e., SharePoint Online or SharePoint OnPremise
(SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
Permissions to access web APIs
Choose
if the components in the solution require permission to access web APIs
that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
Type of client-side component to create
We can choose to create client-side webpart or an extension.
Selected choice: Extension
Type of client-side extension to create
We can choose to create Application customizer, Field customizer, or ListView Command Set.
Selected choice: Application Customizer
Application customizer name
Hit Enter to select the default name or type in any other name.
Selected choice: ReactClockExtension
Application customizer description
Hit Enter to select the default description or type in any other value.
Selected choice: Adds custom header and footer to a SharePoint site
Yeoman
generator will perform scaffolding process to generate the solution.
The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command.
In the command prompt type the below command to open the solution in code editor of your choice.
NPM Packages used:
- npm install react react-dom
- npm i react-live-clock
in ReactClockExtensionApplicationCustomizer.ts
- import { override } from '@microsoft/decorators';
- import {
- BaseApplicationCustomizer,
- PlaceholderContent,
- PlaceholderName
- } from '@microsoft/sp-application-base';
-
- import * as React from "react";
- import * as ReactDOM from "react-dom";
- import ReactHeader ,{IReactHeaderProps} from "./ReactHeader";
- import * as strings from 'ReactClockExtensionApplicationCustomizerStrings';
- export interface IReactClockExtensionApplicationCustomizerProperties {
-
- testMessage: string;
- Top: string;
- }
-
-
- export default class ReactClockExtensionApplicationCustomizer
- extends BaseApplicationCustomizer<IReactClockExtensionApplicationCustomizerProperties> {
- private _topplaceholder: PlaceholderContent | undefined;
- @override
- public onInit(): Promise<void> {
-
-
-
- this.context.placeholderProvider.changedEvent.add(this, this._renderPlaceHolders);
-
-
- this._renderPlaceHolders();
-
- return Promise.resolve();
- }
- private _renderPlaceHolders(): void {
- console.log('Available placeholders: ',
- this.context.placeholderProvider.placeholderNames.map(name => PlaceholderName[name]).join(', '));
-
-
- if (!this._topplaceholder) {
- this._topplaceholder =
- this.context.placeholderProvider.tryCreateContent(
- PlaceholderName.Top,
- { onDispose: this._onDispose });
-
-
- if (!this._topplaceholder) {
- console.error('The expected placeholder (Top) was not found.');
- return;
- }
-
- const elem: React.ReactElement<IReactHeaderProps> = React.createElement(ReactHeader);
- ReactDOM.render(elem, this._topplaceholder.domElement);
- }
- }
- private _onDispose(): void {
- console.log('[ReactHeaderFooterApplicationCustomizer._onDispose] Disposed custom top and bottom placeholders.');
- }
- }
in mystyle.css
-
- @font-face {
- font-family: 'Orbitron';
- font-style: normal;
- font-weight: 400;
- src: url(https:
- unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
- }
- .myclock {
- font-family: 'Orbitron', sans-serif;
- color: #ff6666;
- font-size: 56px;
- text-align: center;
- padding-top: 40px;
- padding-bottom: 40px;
-
- }
-
- #clockdiv{
- text-align:center!important;
- }
-
in ReactHeader.tsx
- import * as React from "react";
- import Clock from 'react-live-clock';
- import './mystyle.css';
- export interface IReactHeaderProps {}
- import { SPComponentLoader } from '@microsoft/sp-loader';
-
- export default class ReactHeader extends React.Component<IReactHeaderProps> {
- constructor(props: IReactHeaderProps) {
- super(props);
- }
-
- public render(): JSX.Element {
- return (
- <div id="clockdiv">
-
- <h1><Clock format={'hh:mm:ssa'} ticking={true} timezone={'Asia/Kolkata'} className="myclock"/></h1>
-
- </div>
- );
- } }
Properties
Properties |
Type |
Default Value |
Description |
date |
timestamp or string |
current date |
Date to output, If nothing is set then it take current date. |
format |
string |
'HH:MM' |
Formatting from moment.js library. |
filter |
function |
(date: String) => date |
Filtering the value before the output . |
timezone |
string |
null |
If timezone is set, the date is show in this timezone. You can find the list. here, the TZ column. |
ticking |
boolean |
FALSE |
If you want the clock to be auto-updated every interval seconds. |
interval |
integer |
1000 |
Auto-updating period for the clock. 1 second is a default value. |
className |
string |
null |
Extra class. |
children |
string |
null |
date can be set as a children prop. |
onChange |
function |
({output, previousOutput, moment}) => {} |
callback function on each output update |
Test the extension
Open serve.json under config folder.
Update the properties section to include page URL to test.
- {
- "$schema": "https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",
- "port": 4321,
- "https": true,
- "serveConfigurations": {
- "default": {
- "pageUrl": "https://contoso.sharepoint.com/sites/mySite/SitePages/myPage.aspx",
- "customActions": {
- "c633afef-a94d-4970-89ca-30f403612550": {
- "location": "ClientSideExtension.ApplicationCustomizer",
- "properties": {
- "testMessage": "Test message"
- }
- }
- }
- },
- "reactClockExtension": {
- "pageUrl": "https://contoso.sharepoint.com/sites/mySite/SitePages/myPage.aspx",
- "customActions": {
- "c633afef-a94d-4970-89ca-30f403612550": {
- "location": "ClientSideExtension.ApplicationCustomizer",
- "properties": {
- "testMessage": "Test message"
- }
- }
- }
- }
- }
- }
On the command prompt, type the below command.
The SharePoint site will open. Click “Load debug scripts”.
Expected output
Hence we learned how to implement digital clock in spfx.
I hope this helps someone. Happy coding :)