GithubHelp home page GithubHelp logo

mrdeerly / vue-timeago3 Goto Github PK

View Code? Open in Web Editor NEW
21.0 2.0 4.0 2.81 MB

A tiny timeago component for Vue 3 using date-fns v2.28.

Home Page: https://mrdeerly.github.io/vue-timeago3/

License: MIT License

TypeScript 100.00%
timeago component vue vue3 vite date-fns time-since since timepassed time-ago

vue-timeago3's Introduction

⏳ vue-timeago3

NPM version NPM downloads code-coverage Build Status Vue version date-fns version size

A time ago component for Vue.js 3 based on vue-timeago for Vue 2 by egoist.

Table of Contents

vue-timeago3 is a tiny component for Vue.js 3, to show the time passed since a specific date. You simply pass a date and get somewhat like 10 seconds ago, 3 weeks ago, ... printed by the component

Example

distance to now result
0 - 5 secs less than 5 seconds ago
5 - 10 secs less than 10 seconds ago
10 - 20 secs less than 20 seconds ago
20 - 40 secs half a minute ago
40 - 60 secs less than a minute ago

See date-fns/formatDistanceToNow for more details.

Visit the docs for more details!

Currently the plugin is available via NPM and Yarn. To install it use one of the two package managers.

// NPM
$ npm install vue-timeago3

// Yarn
$ yarn add vue-timeago3

To register the plugin simply import and register it using the new global vue3 api. As an alternative the plugin could be imported in specific components only.

// src/main.ts
import { createApp } from 'vue'
import timeago from 'vue-timeago3'

const app = createApp(App)
...
app.use(timeago) // register timeago
...
app.mount('#app')

Plugin Options

During the registration of the component you can specify a set of options, which will mutate the plugin globally. If you don't want to define global settings, skip this section and use props instead. To use options, simply pass them during the registration as an object:

// src/main.ts
import { createApp } from 'vue'
import timeago from 'vue-timeago3'

const app = createApp(App)
...
// define options
const timeagoOptions = {
  converterOptions: {
      includeSeconds: false,
  }
}

app.use(timeago,  timeagoOptions) // register timeago with options
...
app.mount('#app')

As of version 1.0.0 the following options are available:

option type description
name string Register the component with a custom name. Default is: timeago
locale Locale (see date-fns/Locale) The locale specifies the language which is used to render the date. All available date-fns locales are supported by default.
converter (date, defaultConvertOptions | strictConverterOptions) => string A converter that formats regular dates in x Seconds ago, or in xxx style. Check out the default converter which uses date-fns formatDistanceToNow
defaultConverterOptions Object Pass some extra settings to the default converter mentioned above. It supports the main options of date-fns, namingly:

includeSeconds - boolean - distances less than a minute are more detailed
addSuffix - boolean - results specifies if now is earlier or later than the date passed
useStrict - false - if true you need to use the strictConverterOptions (see below)
strictConverterOptions Object Pass some extra settings to the default converter mentioned above. It supports the main options of date-fns strict converter, namingly:

useStrict - true - needs to be true, otherwise the defaultConverterOptions have to be used (see above)
addSuffix - boolean - results specifies if now is earlier or later than the date passed
unit - second, minute, hour, day, month, year if specified, will force a unit
roundingMethod - floor, ceil, round which way to round partial units (default=round)

Once the plugin is registered you can straight up use it in your app.

Basic usage:

<template>
  <timeago :datetime="date"/>
</template>

<script>
...
data() {
  return {
    date: '2021-09-01'
  }
}
...
</script

Props

Instead of configurating the plugin during the registration, you can also configuate the component via props.

property type required default description
datetime string | Date | number ✔️ The datetime used to calculate the "time ago"
autoUpdate number | boolean false The period of time to update the component, in seconds.
This can be omitted by setting it to 0 or false.

The default value for true is 60(seconds). Instead of passing true you can also pass a custom time.
locale Locale (see date-fns/Locale) en The locale specifies the language which is used to render the date. All available date-fns locales are supported by default.
converter date, defaultConverterOptions | strictConverterOptions) => string See plugin options above
defaultConverterOptions Object See plugin options above
strictConverterOptions Object See plugin options above

vue-timeago3's People

Contributors

dependabot[bot] avatar gekkedev avatar mrdeerly avatar niekschoemaker avatar srabouin avatar

Stargazers

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

Watchers

 avatar  avatar

vue-timeago3's Issues

v2.2.0 doesn't work with vite

Hi,

the new version doesn't work with vite.

17:03:55 [vite] Internal server error: Failed to resolve entry for package "vue-timeago3". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "vue-timeago3". The package may have incorrect main/module/exports specified in its package.json.
  Plugin: vite:import-analysis
  File: /Users/dspangenberg/Projects/twiceware.cloud/enclosure/src/main.js
      at packageEntryFailure (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:40578:11)
      at resolvePackageEntry (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:40574:9)
      at tryNodeResolve (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:40381:20)
      at Context.resolveId (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:40189:28)
      at async Object.resolveId (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:38754:32)
      at async TransformContext.resolve (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:38528:23)
      at async normalizeUrl (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:56682:34)
      at async TransformContext.transform (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:56837:57)
      at async Object.transform (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:38817:30)
      at async doTransform (/Users/dspangenberg/Projects/twiceware.cloud/enclosure/node_modules/vite/dist/node/chunks/dep-3397b401.js:55749:29)

Greetings,
Danny

Fix automated release workflow

The automated release workflow publishes the package before the actual build is finished, resulting in empty packages.

Empty project folder

Hello, I wanted to try this package but when I yarn add it it'll give me empty project folder:

image

app.config.globalProperties undefined

Hi I'm trying to use vue-timeago3 with Vue.

After following the install instructions I am given an error:
Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'app.config.globalProperties')

I'm using vue 3.2.31, vite 4.0, and bun to install everything. Chances are I'm doing something stupid...

My app.js:

import './bootstrap';
import '../css/map.css';

import { createApp, h } from 'vue';
import App from './MapPages/MapLayout.vue';
import { createPinia } from 'pinia';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';

import timeago from 'vue-timeago3' // import timeago

const timeagoOptions = {
  converterOptions: {
      includeSeconds: true,
  }
}

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)

const app = createApp(App)
app.use(timeago);
app.use(pinia);
app.mount("#app");

And parts of my vue component:

<script setup>
import { ref } from 'vue';
import { settingsStore } from "@/stores/SettingsStore.js";
...
import timeago from 'vue-timeago3'

defineEmits(['close']);

</script>

<template>
...
<timeago :datetime="point.stamp" auto-update />
...
</template>

Types missing

Thank you for an awesome library, but there are no types distributed on npm.

See npm sources

In package json, there ar types specified in dist_types directory which is clearly missing.

Export the component directly

Allow for importing the component directly into where it will be used to allow for easy adoption, code-splitting, lazy-loading, tree-shaking, etc. Being able to globally install a component is useful is some contexts but in most cases you are better off importing it in the specific places using it.

Desired outcome:

<template>
  <TimeAgo :datetime="new Date()" />
</template>

<script>
import { TimeAgo } from 'vue-timeago3';

export default {
  name: 'AnExample',
  components: {
    TimeAgo
  }
};
</script>

Also update README to document this approach once supported. The import statement is the one line that could be changed, as long as the rest works. Like something like this would be fine by me:

import TimeAgo from 'vue-timeago3/dist/TimeAgo.vue';

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.