Code Monkey home page Code Monkey logo

ng2-go-top-button's Introduction

ng2-go-top-button

A simple customizable go-to-top button component for Angular projects

NOTE: For Angular >= 9 refer to the new NPM package https://www.npmjs.com/ng-go-top-button, new versions of Angular will not be supported in this repository any more.

Versions

For Angular >= "5.0.0" use ">=3.0.0", for Angular < "5.0.0" use "^2.0.0" version of this package.

Installation

npm install ng2-go-top-button --save

Configuration

SystemJS

Register @angular/animations and the package itself in systemjs.config.js as following:

System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    map: {
        ...
      'ng2-go-top-button': 'npm:ng2-go-top-button',
      '@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
      '@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
      '@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
    },
    packages: {
        ...
       'ng2-go-top-button': {
            defaultExtension: 'js',
            main: './index.js'
      }
    }

API

Property Type Description
scrollDistance number Number of pixels to be scrolled Y for button to be shown. Defaults to 200px. Must be greater than zero.
html string [Deprecated since v4.0.0] Inner html of button element. Could be an icon or text. Empty by default.
styles object User-defined styles config for the button.
classNames string Custom class names in the following format 'class1 class2 class3'.
animate boolean If true, scrolling will be animated. False by default.
speed number Speedof animated scroll. Must be greater than 1. 80 by default.
acceleration number Number of pixels to speed up when scrolling is animated. 0 by default - this way page will be scrolled top with the constant speed.
tabIndex number Custom tabindex button attribute value, by default 0.

Usage

Import statement:

import {GoTopButtonModule} from 'ng2-go-top-button';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; // angular 4.x and greater

Add it to imports in your module declaration. When using angular 4.x and greater, also import a BrowserAnimationsModule:

@NgModule({
    ...
    imports: [..., GoTopButtonModule, BrowserAnimationsModule],
    ...

If you need to export it from your module, than also add it to exports:

@NgModule({
    ...
    exports: [..., GoTopButtonModule]
    ...

On your template paste the <go-top-button></go-top-button> html. This will add a simple button with default styles and without animated scroll. By default go-top-button will appear on the right side, 50% top and bottom and without any icons or text. You can then customize its styles and behaviour.

Example of customization:

<go-top-button   [animate]="true"
                 [speed]="50"
                 [acceleration]="2"
                 [scrollDistance]="300"
                 [classNames]="'custom-class'"
                 [styles]="{
                    'border-radius': '20px 20px 20px 20px',
                    'right': '5px',
                    'color': 'green',
                    'border': '5px solid',
                    'line-height': '20px'
                 }">
        <i class=\'fa fa-arrow-up\'></i>
  </go-top-button>

Custom CSS class declaration (if needed):

.custom-class {
    position: fixed;
    background-color: pink;
    border-color: green;
    height: 30px;
    width: 30px;
}
Note 1:

For the version lower than 4.0.0 of this package you should still use the old syntax of injecting the html:

<go-top-button [html]="'<i class=\'fa fa-arrow-up\'></i>'" ... ></go-top-button>

The new syntax (injecting via <ng-content>) was introduced with v4.0.0 to fix the Angular Material compatibility issues.

Note 2:

In the version 4.2.0 classNames attribute has been added. Now you can define your custom CSS classes in your parent component and apply them to your go top button. In order to achieve this you need to change the CSS encapsulation level of your parent component to either None or Native:

    import { Component, ViewEncapsulation } from '@angular/core';
    
    @Component({
        ...,
        encapsulation: ViewEncapsulation.None, // ViewEncapsulation.Native may also work
    })

This will let your parent component affect the CSS of the go-top-button child.

The default styles now will not be merged with the input styles values - they have been moved to the default CSS class .go-top-button. So if you override this class with your custom className value, you have to write all CSS yourself with no default styling.

IE-specific issues:

IE does not support web animations. If you would like to enable them, install and import the corresponding polyfill to your polyfills.js file:

In your project's directory:

npm install --save web-animations-js

In polyfills.js:

import 'web-animations-js';

Example project

You can find example project that uses ng2-go-top-button in example-app/ directory of this repository. Navigate to this directory and run npm run start, the app will be hosted on http://localhost:4200 (is this port is not taken by any other apps on your host).

Issues & Pull Requests

Feel free to create issues and pull requests. Even though there is a 3.x version of this module for Angular 5, I still support the 2.x version for Angular 4 and lower. Fixes for 2.x version go to the v2-fixes branch.

ng2-go-top-button's People

Contributors

annapogorelova avatar dependabot[bot] avatar mriel avatar pacoita avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ng2-go-top-button's Issues

error

capture

got error like this..

Add Angular 9 support

Does not work with Angular 9:

Compiling ng2-go-top-button : main as commonjs
ERROR in getInternalNameOfClass() called on a non-ES5 class: expected GoTopButton to have an inner class declaration

Would it be compatible to make a new version compatible with Angular 9?

Thanks a lot!

AOT Build

After when I invoked this call from CLI:

"ng build -prod -aot -vc -cc -dop --buildOptimizer"

after I've got:

"ERROR in : Can't bind to 'classNames' since it isn't a known property of 'go-top-button'.

  1. If 'go-top-button' is an Angular component and it has 'classNames' input, then verify that it is part of this module.
  2. If 'go-top-button' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
  3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
    <go-top-button [animate]="true" [speed]="20" [acceleration]="5" [scrollDistance]="300" [ERROR ->][classNames]="'arrow-go-to-top active'">

    </go-top-button")"

My component.ts has:

@component({
'other things'
encapsulation: ViewEncapsulation.None
})

In development mode everything works right. Any suggestions ??

Cannot update to Angular 7.x

Hello,
I have a problem when i try to upgrade project to Angular 7.x, because it's not compatible with typescript 3.1.6..
"Package "ng2-go-top-button" has an incompatible peer dependency to "typescript" (requires "^2.x", would install "3.1.6")."
"Incompatible peer dependencies found. See above."

It works only if i use '--force'

Any suggestions, please?
Thank you advice!

ng build --prod and ng serve --aot fail

Hello, I tried to deploy my app with this component using ng build --prod, and got an error. I also got the same error with ng serve --aot:

anselhalliburton@xyz:~/Desktop/ng2$ ng serve --aot
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2018-03-09T18:05:05.892Z                                                       
Hash: e71112c02aaac8501dd3
Time: 5008ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 2.93 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 636 bytes [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 185 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 853 kB [initial] [rendered]

ERROR in : Unexpected value 'GoTopButtonModule in /Users/anselhalliburton/Desktop/ng2/node_modules/ng2-go-top-button/dist/src/go-top-button.module.d.ts' imported by the module 'AppModule in /Users/anselhalliburton/Desktop/ng2/src/app/app.module.ts'. Please add a @NgModule annotation.

For readability, the important part is this:

ERROR in : Unexpected value 'GoTopButtonModule in /Users/anselhalliburton/Desktop/ng2/node_modules/ng2-go-top-button/dist/src/go-top-button.module.d.ts' imported by the module 'AppModule in /Users/anselhalliburton/Desktop/ng2/src/app/app.module.ts'. Please add a @NgModule annotation.

I have poked around on Stack Overflow, etc., but am not really following the proposed solutions. This question and answer seem helpful, though: Angular CLI - Please add a @NgModule annotation when using latest.

'SafeHtml' is declared but never used.

ERROR in node_modules/ng2-go-top-button/src/safe-html.pipe.ts(1,23): error TS6133: 'SafeHtml' is declared but never used.

Anyone encounter this issue?

Angular Material <mat-icon> doesn't work

Nice package! I am using it in a project with Angular Material, and would like to use an icon from the Material Icons set. This is done with a <mat-icon> component. However, it doesn't work.

<go-top-button
  [animate]="true"
  [speed]="100"
  [acceleration]="2"
  [html]="'<mat-icon>expand_less</mat-icon>'"
  [styles]="{
    'right': '10px',
    'bottom': '10px'
   }"
></go-top-button>

Everything except the icon works just fine. Here's what renders:
image

Any ideas? Thanks!

Default attribute styles

Hi,

I think you can extend your awesome directive regard styles. You've defined default styles like this:

this.defaultStyles = {
'position': 'fixed',
'top': '50%',
'bottom': '50%',
'right': '0',
'width': '35px',
'height': '35px',
'line-height': '35px',
'text-decoration': 'none',
'color': 'white',
'background': 'rgba(0, 0, 0, 0.3)',
'border': 'none',
'border-radius': '3px 0 0 3px'
};

In my case I do not need your default styles. I think that should be optional.
That would be great if you can implement the following three cases

  1. If you do not define any styles the attribute style should not be rendered on html tag.
  2. That option will be the best for me. If I can put names of classes from css in current button attribute i.e: [styles]={ 'classA classB classC' }
  3. The style configuration that works as standard functionality now.

What do you think ?? This is only my proposal ;)

No provider for DomSanitizer

ERROR Error: StaticInjectorError[DomSanitizer]: StaticInjectorError[DomSanitizer]: NullInjectorError: No provider for DomSanitizer!

Getting this error now with 5.0.0-beta.2 and CLI 1.5.0

@NgModule({ imports: [CommonModule], declarations: [GoTopButton, Safe], exports: [GoTopButton] })

Button not working on iPhone

I integrated the button in my Angular app. It works fine on desktop however on mobile devices (specifically iPhone) the button does not work as expected. After clicking on the button, the window scrolls to top but is stuck in the same position. It does not scroll down.

It is not working if we are using inside component in angular

If I use the go-top-button inside component as we are using top:50% in the code It will be at half of the page. We have to change it to top:auto so that it goes to the button of the page

<go-top-button [animate]="true"
[speed]="50"
[acceleration]="2"
[scrollDistance]="300"
[styles]="{
'top' : 'auto',
'opacity':'1!important',
'background': 'transparent'
}">

This didn't I had to go and change it inside go-top-button.component.js

Unable to get keyboard focus

Hi,

I am using this control and it works great but there seems to be no way to get keyboard focus on the item. i have tried putting tabindex='1' and adding a click handler to the nothing has worked.

Here is my code:

<go-top-button [animate]="true"
[speed]="50"
[acceleration]="2"
[scrollDistance]="350"
[classNames]="'go-top-scrolling'"
tabindex='1'
role='button'
[styles]="{
'border-radius': '20px 20px 20px 20px',
'right': '15px',
'bottom': '25px',
'color': '#333',
'border': '4px solid',
'line-height': '20px'
}">
<span tabindex='1' class='fa fa-arrow-up' >

Please help, if I cannot make this accessible I will have to take it off...

Regards,
Prashant.

Remove Typescript peer dependency

Hi,

would it be possible to remove the typescript peer dependency from the package.json?

"peerDependencies": {
"@angular/animations": "^8.0",
"@angular/common": "^8.0",
"@angular/core": "^8.0",
"@angular/platform-browser": "^8.0",
"rxjs": "^6.0",
"typescript": "~3.4.0",
"zone.js": "^0.9.0"
},

you already have a dev dependency over typescript:
"devDependencies": {
"@angular/animations": "^8.0.3",
"@angular/common": "^8.0.3",
"@angular/compiler": "^8.0.3",
"@angular/compiler-cli": "^8.0.3",
"@angular/core": "^8.0.3",
"@angular/platform-browser": "^8.0.3",
"@types/core-js": "^2.5.0",
"rxjs": "^6.5.2",
"tsickle": "^0.34.3",
"typescript": "^3.4.5",
"zone.js": "^0.9.1"
}

This blocks me to upgrade ng-bootstrap.

Accessibility

There seems to be an issue when trying to add accessibility aria attributes to the HTML. I am currently trying to do this:

[html]="'<i class=\'fa fa-arrow-up\' aria-hidden=\'true\' aria-label=\'go to top of the page\'></i>'"

but the attributes are not showing up in the output HTML.

tsconfig.json error message

Hi Anna,
I'm getting this error message (see below) when I added the "include" path to the tsconfig.json . I am also including my tsconfig.json file. Please can you help me to resolve this error. I appreciate your help.
Thanks,
Marvin

tsconfig.json:
{
"compileOnSave": false,
"include": [
"node_modules/ng2-go-top-button/src/**/*"
],
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
],
"types": [
"jasmine",
"firebase"
]
}
}

ng serve --port=5000
Your global Angular CLI version (1.6.3) is greater than your local
version (1.5.5). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
error TS18003: No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["node_modules/ng2-go-top-button/src//*"]' and 'exclude' paths were '["tes
t.ts","
/*.spec.ts"]'.

Error: error TS18003: No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["node_modules/ng2-go-top-button/src//*"]' and 'exclude' paths were
'["test.ts","
/*.spec.ts"]'.

at AngularCompilerPlugin._setupOptions (C:\Users\multi\Desktop\portfolioWeb\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:90:19)
at new AngularCompilerPlugin (C:\Users\multi\Desktop\portfolioWeb\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:43:14)
at _createAotPlugin (C:\Users\multi\Desktop\portfolioWeb\node_modules\@angular\cli\models\webpack-configs\typescript.js:77:16)
at Object.getNonAotConfig (C:\Users\multi\Desktop\portfolioWeb\node_modules\@angular\cli\models\webpack-configs\typescript.js:100:19)
at NgCliWebpackConfig.buildConfig (C:\Users\multi\Desktop\portfolioWeb\node_modules\@angular\cli\models\webpack-config.js:37:37)
at Class.run (C:\Users\multi\Desktop\portfolioWeb\node_modules\@angular\cli\tasks\serve.js:71:98)
at check_port_1.checkPort.then.port (C:\Users\multi\Desktop\portfolioWeb\node_modules\@angular\cli\commands\serve.js:123:26)
at process._tickCallback (internal/process/next_tick.js:109:7)

Upgrade to Angular 8.x

Could you please upgrade to Angular 8.x ?

Component itself works fine after upgrading whole app
But there are some warnings

npm WARN [email protected] requires a peer of @angular/animations@^7.x but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of @angular/common@^7.x but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of @angular/core@^7.x but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of @angular/platform-browser@^7.x but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of typescript@>=3.1.1 <3.3.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of zone.js@^0.8.x but none is installed. You must install peer dependencies yourself.

Best Regards

Error in Angular 5.0.1/CLI 1.5

ERROR in Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function (position 194:50 in the original .ts file), resolving symbol NgModule in C:/Users/jjamieson/Desktop/Shared/VSIP-birth-angular/BirthRegistryUI/node_modules/ng2-go-top-button/node_modules/@angular/core/core.d.ts, resolving symbol GoTopButtonModule in C:/Users/jjamieson/Desktop/Shared/VSIP-birth-angular/BirthRegistryUI/node_modules/ng2-go-top-button/src/go-top-button.module.ts, resolving symbol GoTopButtonModule in C:/Users/jjamieson/Desktop/Shared/VSIP-birth-angular/BirthRegistryUI/node_modules/ng2-go-top-button/src/go-top-button.module.ts

Getting this error now with the latest version of Angular. You may want to consider updating the package's local Angular version as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.